2010-05-11 59 views
4

我有一個java代碼來讀取XML節點,我想添加並且想要讀取父節點值。如何在android中讀取XML父節點標籤值

我的XML文件樣品低於:

<breakfast_menu><food id=1><name> Belgian Waffles </name><price> $5.95 </price><description> two of our famous Belgian Waffles with plenty of real maple syrup </description><calories> 650 </calories></food><food id=2><name>Strawberry Belgian waffles</name><price>$7.95</price><description>light Belgian waffles covered with strawberries and whipped cream</description><calories>900</calories></food></breakfast_menu> 

和我解析XML代碼:

public static String getProductItem(String pid, String item) { 

    try { 
     url = new URL(""); 
     urlConnection = url.openConnection(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 

    } 

    try { 
     dBuilder = dbFactory.newDocumentBuilder(); 
    } catch (ParserConfigurationException e) { 
    } 
    try { 
     doc = dBuilder.parse(urlConnection.getInputStream()); 
    } catch (SAXException e) { 

    } catch (IOException e) { 

    } 

    doc.getDocumentElement().normalize(); 

    NodeList nList = doc.getElementsByTagName("food"); 

    for (int temp = 0; temp < nList.getLength(); temp++) { 

     Node nNode = nList.item(temp); 

     if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

      Element eElement = (Element) nNode; 



       data = getTagValue(item, eElement); 



     } 
    } 

    doc = null; 
    dBuilder = null; 

    return data; 

} 


private static String getTagValue(String sTag, Element eElement) { 
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0) 
    .getChildNodes(); 
    Node nValue = (Node) nlList.item(0); 

    return nValue.getNodeValue(); 
} 

我想要做的是閱讀食品的「ID」值,所以如果我正在尋找一種食物,它只會檢查那些與食物節點ID相匹配的食物節點。

如果我通過標籤ID讀取,它會讀取所有標籤,而不是一個標籤。如何實現它?

+0

注意:我不知道如何在android中使用Xpath;所以建議其他選項 – 2012-12-01 09:32:14

回答

4

也許我錯過了一些東西,難道只是你要找的String id = eElement.getAttribute("id");

您可能想要考慮使用XPath從您的DOM中取出。 getElementsByTagName具有忽略文檔結構的煩人習慣。 (也應該使用名稱空間和getElementsByTagNameNS :))

+0

我不能在android中使用Xpath,我想堅持使用DOM。有什麼方法可以獲得DOM中的屬性? – kaibuki 2010-05-12 10:27:08

4

您可以使用XPath根據某些條件輕鬆找到一組節點。 例如:

XPath xpath = XPathFactory.newInstance().newXPath(); 
NodeList nl = (NodeList) xpath.evaluate("//food[@id=22]", doc, XPathConstants.NODESET); 

此代碼中找到與文檔中給出的ID的所有食品節點。 XPath是這種搜索條件的豐富語言。

+1

我不能在Adnroid中使用Xpath,有沒有什麼辦法可以在上面提到的代碼中做到這一點。 – kaibuki 2010-05-12 10:25:28

+0

對不起,但爲什麼你不能使用它?我之前在Android上使用過XPath,沒有任何問題。 – kaneda 2012-12-05 22:23:55

3

XPath可以因爲API 8級。在Android中使用。如果這是你的選擇,假設你的XML被放置在您的項目在

/res/raw/food_list.xml

鑑於這種情況,你可以搜索使用此XPath語法的所有元素:

//food/@id 

上述搜索表達式將返回所有的列表屬於<food>元素的屬性,無論它們是從哪裏開始的。

所以,如果你想要的是獲取所有這些id屬性,您可以按如下

InputStream is = getResources().openRawResource(R.raw.food_list); 
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); 

try { 
    Document doc = fac.newDocumentBuilder().parse(is); 
    XPath xpath = XPathFactory.newInstance().newXPath(); 
    XPathExpression expr; 
    try { 
     expr = xpath.compile("//food/@id"); 
     NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); 
     for (int i = 0; i < nl.getLength(); i++) { 
       Node node = nl.item(i); 
       // Considering the example XML at the end of this loop you will 
       // get the ids printed 
       System.out.println(node.getNodeValue()); 
     } 
    } catch (XPathExpressionException e) { 
     e.printStackTrace(); 
    } 
    is.close(); 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} catch (SAXException e) { 
    e.printStackTrace(); 
} catch (ParserConfigurationException e) { 
    e.printStackTrace(); 
} 

XPath語法快速參考,稍微解釋一下,可以發現here做到這一點。

+0

真的很好解釋;幫助我了很多:) thanx :) – 2012-12-07 08:51:47

+0

很高興知道,Manoj我很高興我能幫忙:) – kaneda 2012-12-08 12:47:20