我的Xml响应中有这种数据来自服务器,它以“NewDataSet”标签开头和结尾,在这个标签下有几个“Table”标签。并且在这个表标签中我想从所有Table标签中获取一些字段的值,比如说CallDate,CallDuration。这样我就可以相应地显示数据。但是在网上的所有教程中,他们都在显示解析后的数据。所以我的问题是如何从这个Xml中的所有表标签中获取这些特殊字段中的数据。我确定我的问题非常清楚。请帮帮我
<NewDataSet>
<Table>
<CallDetailId>06012015192014733680011</CallDetailId>
<CallID>83A26D3811000100</CallID>
<CorrelatorId />
<EncryptedCall>false</EncryptedCall>
<CallDate>2015-01-06 19:20:14</CallDate>
<CalledBy>337</CalledBy>
<CalledTo>368</CalledTo>
<IsExternal>false</IsExternal>
<RecorderType>PRIMARY</RecorderType>
<FirstName />
<LastName />
<Comments />
<FlagID>1</FlagID>
<FlagImage>1.gif</FlagImage>
<CallDirection>I</CallDirection>
<FLAGTEXT>Training required</FLAGTEXT>
<Archive>true</Archive>
<ExtensionValue>368</ExtensionValue>
</Table>
<Table>
<AgentHangup>true</AgentHangup>
<CallDetailId>060120151424080020030003</CallDetailId>
<CallID>1F5D6D3803000100</CallID>
<CorrelatorId />
<CallDuration>5</CallDuration>
<EncryptedCall>false</EncryptedCall>
<CallDate>2015-01-06 14:24:08</CallDate>
<CalledBy>2003</CalledBy>
<CalledTo>210</CalledTo>
<IsExternal>false</IsExternal>
<RecorderType>PRIMARY</RecorderType>
<FirstName />
<LastName />
<Comments />
<FlagID>1</FlagID>
<FlagImage>1.gif</FlagImage>
<AF>C:\Program Files (x86)\Alcatel-Lucent\OmniPCXRecord Suite\OmniPCXRecord\OmniPCXRECORDAudio\Repository\from\150106\0207\202_06012015 142408003_0003_2003.wav</AF>
<CallDirection>O</CallDirection>
<FLAGTEXT>Training required</FLAGTEXT>
<Archive>false</Archive>
<ExtensionValue>2003</ExtensionValue>
<GraphValue />
</Table>
我这样做
result = xmlStringComingFromServer
Document dom = null;
DocumentBuilder builder;
DocumentBuilderFactory factory;
try {
InputStream resultStream = new ByteArrayInputStream(results.getBytes("utf-8"));
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
dom = builder.parse(resultStream);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (dom != null) {
Log.v("Dom is Not Null", dom.toString() + "");
NodeList nodeList = dom.getElementsByTagName("NewDataSet");
/*
putting new data here
*/
menuItems = new ArrayList<HashMap<String, String>>();
// NodeList nl = dom.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nodeList.getLength(); i++) {
// creating new HashMap
Log.v("For Rounds",i+"");
Element e = (Element)nodeList.item(i);
// adding each child node to HashMap key => value
map.put("Table",e.getElementsByTagName("Table").item(i).getTextContent());
//map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
// map.put("CallDuration",e.getElementsByTagName("CallDuration").item(0).getTextContent());
// map.put("CalledBy",e.getElementsByTagName("CalledBy").item(0).getTextContent());
// map.put("CalledTo",e.getElementsByTagName("CalledTo").item(0).getTextContent());
// map.put("FirstName",e.getElementsByTagName("FirstName").item(0).getTextContent());
// map.put("LastName",e.getElementsByTagName("LastName").item(0).getTextContent());
// adding HashList to ArrayList
menuItems.add(map);
}
答案 0 :(得分:1)
您可以使用XmlPullParser来更快地解析xml,并且比加载Dom更有效。
try {
XmlPullParser xpp;
XmlPullParserFactory factory = null;
factory = XmlPullParserFactory.newInstance();
xpp = factory.newPullParser();
xpp.setInput(mInput, "UTF-8");
String N = ""; //I know it's capitalized but that's how I do temp strings.
int eventType = xpp.getEventType();
while (true) {
if (eventType == XmlPullParser.START_TAG) {
// code taken from AsyncTask check cancel each loop your code
// may not need this check
if (isCancelled()) {
return RESULT_ABORTED;
}
N = xpp.getName();
if (N.equals("CallDate")) {
String callDate = xpp.getText();
} else if (N.equals("somethingelse")) {
String somethingelse = xpp.getText();
}
} else if (eventType == XmlPullParser.END_DOCUMENT) {
break;
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
ret = RESULT_ERROR;
} catch (IOException e) {
ret = RESULT_ERROR;
e.printStackTrace();
}