@墨者1906: @林J:
System.err.println("要解析的XML是:"+result);
sText.setText("服务器返回的XML: " + result);
//响应的结果为XML 开始解析
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
inputStreamA = null;
factory = DocumentBuilderFactory.newInstance();
inputStreamA = new ByteArrayInputStream(result.getBytes());
factory = DocumentBuilderFactory.newInstance();
// 得到一个DocumentBuilder解析类
builder = factory.newDocumentBuilder();
// 接收一个xml的字符串来解析xml,Document代表整个xml文档
document = builder.parse(inputStreamA);
// 得到xml文档的根元素节点
Element personsElement = document.getDocumentElement();
// 得到标签为PDAExchange的Node对象的集合NodeList
NodeList nodeList = personsElement.getElementsByTagName("PDAExchange");
System.err.print("nodeListl集合的有:"+nodeList.getLength()+"个元素");
for (int i = 0; i < nodeList.getLength(); i++) {
// 如果该Node是一个Element
if (nodeList.item(i).getNodeType() == Document.ELEMENT_NODE) {
Element personElement = (Element) nodeList.item(i);
// 得到Node的属性值
String Nodname = personElement.getAttribute("returnMsg");
System.err.print(Nodname);
NodeList childNodesList = personElement.getChildNodes();
for (int j = 0; j < childNodesList.getLength(); j++) {
if (childNodesList.item(j).getNodeType() == Document.ELEMENT_NODE) {
// 解析到了person下面的name标签
if ("Vid".equals(childNodesList.item(j).getNodeName())) {
// 得到name标签的文本值
String name = childNodesList.item(j).getFirstChild().getNodeValue();
System.err.print(name);
} else if ("Authority".equals(childNodesList.item(j).getNodeName())) {
String age = childNodesList.item(j).getFirstChild().getNodeValue();
System.err.print(age);
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
inputStreamA.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}