In my android application (Minimum SDK version 4) have to communicate with a server to get some data and receive them as a XML response stream. Following is a sample response
在我的Android应用程序(Minimum SDK version 4)中,必须与服务器通信以获取一些数据并将其作为XML响应流接收。以下是样本回复
<Orders>
<Orders>
<OrderID>1000</OrderID>
<OrderDate>20/04/2015 11:35:50</OrderDate>
<Reference>ttt</Reference>
<Amount>12.50</Amount>
<Status>0</Status>
<CustomerName>John</CustomerName>
<Address>30, George Street, Middlesex</Address>
</Orders>
<Orders>
<OrderID>1001</OrderID>
<OrderDate>20/04/2015 10:54:35</OrderDate>
<Reference>ABC</Reference>
<Amount>40.50</Amount>
<Status>Pending</Status>
<CustomerName>Peter Smith</CustomerName>
<Address></Address>
</Orders>
</Orders>
I need to Parse using Java XML DOM Parser but finding it difficult to read the text content. Getting a waring message saying
我需要使用Java XML DOM Parser进行解析,但发现难以阅读文本内容。得到一条警告信息说
"Call requires API level 8 (current min is 4): org.w3c.dom.Node#getTextContent"
“调用需要API级别8(当前最小值为4):org.w3c.dom.Node#getTextContent”
When use following line of code
使用以下代码行时
element.getElementsByTagName("Status").item(0).getTextContent();
Therefore I tried following way and working alright.
因此,我尝试了以下方式,并正常工作。
element.getElementsByTagName("Status").item(0).getFirstChild().getNodeValue().trim()
But I need to check if [element.getElementsByTagName("Status").item(0) != null]
但我需要检查是否[element.getElementsByTagName(“Status”)。item(0)!= null]
That's alright But when it's empty for example <Status></Status>
need to check if [element.getElementsByTagName("Status").item(0).getFirstChild() != null]
as well...
那没关系但是当它为空时,例如
I am sure there must be a proper way to do this. But didn't find any sample code explaining this. If anyone know the best way to do this (using DOM parser for minimum support SDK 4) would like to hear. Thanks
我相信必须有一个正确的方法来做到这一点。但是没有找到解释这个的示例代码。如果有人知道这样做的最佳方法(使用DOM解析器获得最低支持SDK 4)希望听到。谢谢
2 个解决方案
#1
0
Do you actually need to use a DOM parser? For most xml, I find XStream or similar parsers a lot easier to use.
你真的需要使用DOM解析器吗?对于大多数xml,我发现XStream或类似的解析器更容易使用。
#2
0
try the code below
尝试下面的代码
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public void checkXML()
{
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
String str="<o> <Orders>"+
"<OrderID>1000</OrderID>"+
"<OrderDate>20/04/2015 11:35:50</OrderDate>"+
"<Reference>ttt</Reference>"+
"<Amount>12.50</Amount>"+
"<Status>0</Status>"+
"<CustomerName>John</CustomerName>"+
"<Address>30, George Street, Middlesex</Address>"+
"</Orders></o>";
xpp.setInput( new StringReader(str));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}
catch(Exception e)
{
e.printStackTrace();
}
}
#1
0
Do you actually need to use a DOM parser? For most xml, I find XStream or similar parsers a lot easier to use.
你真的需要使用DOM解析器吗?对于大多数xml,我发现XStream或类似的解析器更容易使用。
#2
0
try the code below
尝试下面的代码
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public void checkXML()
{
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
String str="<o> <Orders>"+
"<OrderID>1000</OrderID>"+
"<OrderDate>20/04/2015 11:35:50</OrderDate>"+
"<Reference>ttt</Reference>"+
"<Amount>12.50</Amount>"+
"<Status>0</Status>"+
"<CustomerName>John</CustomerName>"+
"<Address>30, George Street, Middlesex</Address>"+
"</Orders></o>";
xpp.setInput( new StringReader(str));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}
catch(Exception e)
{
e.printStackTrace();
}
}