我試圖將this XML解析到我的Android應用程序中。 我在這裏讀了很多關於在stackoverflow上解析XML的問題,但我不確定如何處理每個人提到的for
循環,就我而言。將XML解析爲Android應用程序
如果有人願意給我一個這個XML案例的實例,我會非常感激。
示例代碼我發現計算器
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(*/Insert my URL/*);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName(*/What to write here in my case?/*);
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(*/What to write here in my case?/*);
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);
}
上面的代碼是NOT,我使用的例子。這正是我在互聯網上發現的如何在Android應用上解析XML。但是,我的XML似乎比這裏的貼子更復雜。
你能不能給我一個鏈接或一個簡短的解釋,我怎麼會在這裏使用JSON? 我知道JSON是什麼,因爲我使用它。但是現在它讓我困惑,因爲我不知道如何使用JSON在我的應用程序上顯示該XML。 – user3533253 2014-10-08 16:20:12
你不能在這裏使用JSON,我的錯誤。這裏是一個指南,看起來很容易遵循 - http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ – apmartin1991 2014-10-08 16:22:43