【问题标题】:Checking if particular node exists in Xml file using Xpath使用 Xpath 检查 Xml 文件中是否存在特定节点
【发布时间】:2012-07-25 13:21:24
【问题描述】:

我想使用 xPath 检查我的 xml 文件中是否存在 code = "ABC" 。您能建议我一些方法吗?

<metadata>
 <codes class = "class1">
      <code code = "ABC">
            <detail "blah blah"/>
        </code>
  </codes>
  <codes class = "class2">
      <code code = "123">
            <detail "blah blah"/>
        </code>
  </codes>
 </metadata>

[编辑] 我做了以下。它返回 null。

            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xPath.compile("//codes/code[@ code ='ABC']");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);

            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println("nodes: "+ nodes.item(i).getNodeValue()); 
            }

【问题讨论】:

  • 你试过什么?你搜索过什么?你的障碍到底在哪里?
  • 这不是有效的 xml,&lt;code="ABC"&gt; 缺少属性名称
  • 你的匹配模式“@code =”中真的有空格吗?如果是这样,请删除那些额外的空格。此外,元素的“nodeValue”为“null”,因此您的代码实际上可能正在工作。你应该打印出更有意义的东西。

标签: java xml xpath


【解决方案1】:

我不知道您是如何测试您的代码的,因为 &lt;detail "blah blah"/&gt; 是一个不正确的 xml 构造,它应该是 &lt;detail x="blah blah"/&gt; 即一个 name-value 对!

对于 XPath 表达式 "//codes/code[@ code ='ABC']"nodes.item(i).getNodeValue()) 将是 null,因为它将返回一个元素。请参阅下面的 Javadoc 注释:

A working sample:

import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Test 
{
    public static void main(String[] args) throws Exception
    {
        Document doc = getDoc();
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;
        System.out.println("Have I found anything? " + (nodes.getLength() > 0 ? "Yes": "No"));

        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println("nodes: "+ nodes.item(i).getNodeValue()); 
        }

    }

    private static Document getDoc() 
    {
        String xml = "<metadata>"+
                 "<codes class = 'class1'>"+
                      "<code code='ABC'>"+
                            "<detail x='blah blah'/>"+
                        "</code>"+
                  "</codes>"+
                  "<codes class = 'class2'>"+
                      "<code code = '123'>"+
                            "<detail x='blah blah'/>"+
                        "</code>"+
                  "</codes>"+
                 "</metadata>";


        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            DocumentBuilder db = dbf.newDocumentBuilder();
            Document dom = db.parse(new ByteArrayInputStream(xml.getBytes()));
            return dom;

        }catch(Exception pce) {
            pce.printStackTrace();
        }
        return null;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多