1
林有一個很難試圖找出如何解析數據的塊:的Android XML解析「嵌入值」
<prov version="1.1">
<characteristic type="section1">
<parm name="version" value="74"/>
<parm name="validity" value="172800"/>
</characteristic>
...
<characteristic type="section99">
<parm name="random_setting1" value="blahblah1"/>
<parm name="random_setting2" value="blahblah2"/>
<characteristic type="section99_subsection2">
<parm name="random_setting3" value="blahblah1"/>
<parm name="random_setting4" value="blahblah2"/>
</characteristic>
</characteristic>
</prov>
我有可能200+線在XML文件類似於上述示例。 Id喜歡它分解成4個領域放在一個數據庫:
1stlevel特徵
2ndlevel特性(可以爲null)
settingName
值
但我不能爲我的生活弄清楚如何做到這一點。
我有這個文檔構建:
Log.d("RNM", "Starting xmlToDb");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document xmlParse = dBuilder.parse(new InputSource(new StringReader(xmlString)));
xmlParse.getDocumentElement().normalize();
NodeList nList = xmlParse.getElementsByTagName("characteristic");
for (int tmp = 0; tmp < nList.getLength(); tmp++) {
Node nNode = nList.item(tmp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String charType = eElement.getAttribute("type"); //Tells me the value of characteristic
}
}
上述作品拔出所有特性塊,並且能夠得到的值。但我不知道如何提取每個之下的parmName和parmValud。
有沒有處理這個問題的例子?我看了這裏:http://theopentutorials.com/tutorials/android/xml/android-simple-xml-sax-parser-tutorial/但我無法弄清楚如何用saxparser獲取這些值。
Rieekan,這讓我在正確的軌道上,有什麼儘管缺少的是獲得了「嵌入式項」的值(不知道他們叫什麼)是xpp.getAttributeCount,然後使用getAttributeValue遍歷該數字來構建我的dbstring。當我從我的例子中得到我需要的每個特徵部分的值時,next和nextToken幫助我們繼續前進。 –