我是Android Native编码的新手,我有来自我的webservice的xml响应,我很困惑。如何解析xml数据并将数据插入到String数组中..我有一个来自服务器的响应。
<?xml version="1.0" encoding="UTF-8"?>
<soap:Body>
<GetActiveThemeListResponse xmlns="http://tempuri.org/">
<GetActiveThemeListResult>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ActiveThemeList">
<xs:complexType>
<xs:sequence>
<xs:element name="ThemeName" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<NewDataSet xmlns="">
<ActiveThemeList diffgr:id="ActiveThemeList1" msdata:rowOrder="0">
<ThemeName>sample</ThemeName>
</ActiveThemeList>
<ActiveThemeList diffgr:id="ActiveThemeList2" msdata:rowOrder="1">
<ThemeName>magic</ThemeName>
</ActiveThemeList>
<ActiveThemeList diffgr:id="ActiveThemeList3" msdata:rowOrder="2">
<ThemeName>travel</ThemeName>
</ActiveThemeList>
</NewDataSet>
</diffgr:diffgram>
</GetActiveThemeListResult>
</GetActiveThemeListResponse>
</soap:Body>
</soap:Envelope>
如何获取第二个子节点NewDataSet
的数据
我需要将ThemeName
节点值sample
插入到array []中;
我需要存储如下数据:
Ex:[sample, magic, travel]
AnyBody帮助将不胜感激......
答案 0 :(得分:0)
你可以使用Jsoup
(从here下载 - 只需将jar添加到您的项目中。如果您遇到任何问题,请告诉我们)
要使用它,你会写:
List<String> themes = new ArrayList<String>();
// Parse the XML to create a DOM
Document doc = Jsoup.parse(yourXmlAsAString);
// Use the select method to select all ThemeName tags
for (Element elem: doc.select("ThemeName")){
// Use the text method to get the inner text
themes.add(elem.text());
}
如果您想确保Jsoup仅选择ThemeName
的{{1}}个标记,请替换
NewDataSet
与
for (Element elem: doc.select("ThemeName")){
答案 1 :(得分:0)
另一个选择是使用XmlPullParser。与SAX和DOM相比,它的优点是相对简单的API,并且速度快,并且需要比DOM API更少的内存。请阅读the google doc here 示例如何使用
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException.html;
import org.xmlpull.v1.XmlPullParserFactory;
public class SimpleXmlPullApp
{
public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
System.out.println("End 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();
}
}
}