【问题标题】:How to read XML parent node tag value in android如何在android中读取XML父节点标签值
【发布时间】:2010-05-11 07:29:33
【问题描述】:

我有一个读取 XML 节点的 java 代码,我想添加,也想读取父节点值。

我的 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 匹配的食物节点。

如果我按标签 id 读取,它会读取所有标签,而不是一个标签。如何实现?

【问题讨论】:

  • 注意:我不知道如何在 android 中使用 Xpath;所以建议一些其他的选择请

标签: android android-xml


【解决方案1】:

也许我错过了什么,您正在寻找的只是 String id = eElement.getAttribute("id"); 吗?

您可能想研究使用 XPath 从您的 DOM 中提取数据。 getElementsByTagName 有一个忽略文档结构的恼人习惯。 (也应该使用命名空间和 getElementsByTagNameNS :))

【讨论】:

  • 我不能在 android 中使用 Xpath,我想坚持使用 DOM。有什么方法可以获取 DOM 中的属性?
【解决方案2】:

您可以使用 XPath 根据某些标准轻松找到一组节点。 例如:

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

此代码查找文档中具有给定 id 的所有食物节点。 XPath 是这种搜索条件的丰富语言。

【讨论】:

  • 我不能在 Adnroid 中使用 Xpath,有什么办法可以在上面提到的代码中做到这一点。
  • 不好意思问,为什么不能用呢?我之前在 Android 上使用过 XPath,没有任何问题。
【解决方案3】:

XPath 自 API 级别 8 起可在 Android 中使用。如果您可以选择,假设您的 xml 放在您的项目中

/res/raw/food_list.xml

鉴于此,您可以使用以下 xpath 语法搜索所有元素:

//food/@id

上述搜索表达式将返回与 &lt;food&gt; 元素相关的所有 id 属性的列表,无论它们是从根开始的。

因此,如果您想要获取所有这些 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();
}

可以在here 找到有关 XPath 语法的快速参考和一些解释。

【讨论】:

  • 很高兴知道,Manoj 我很高兴能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
相关资源
最近更新 更多