我编写了这段代码来解析XML字符串,但它不起作用。没有错误,但没有给出任何输出。
我的 XML 字符串:
String xml = "<ArrayOfString>" + "<string>{" + "categoryId"+":1," +
"Title" +":"+"Rock"+"," + "songs" +":" +" null}</string>" + "</ArrayOfString>";
编码:
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("ArrayOfString");
item = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
item[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList itemList = fstElmnt.getElementsByTagName("string");
Element itemElement = (Element) itemList.item(0);
itemList = itemElement.getChildNodes();
item[i].setText("item = "
+ ((Node) itemList.item(0)).getNodeValue());
layout.addView(item[i]);
}
}
catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}