使用Java在文档中的任意位置查找XML元素

使用Java在文档中的任意位置查找XML元素,java,xml,xpath,Java,Xml,Xpath,给定以下XML(示例): 但是,这不会输出任何内容。我尝试了以下XPath表达式: //变体 //变式/文本() //rsb:变体 //rsb:变体/文本() 正确的XPath表达式是什么?或者有没有更简单的方法来获取此元素?我建议只需在文档中循环查找给定的标记 public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException, TransformerExc

给定以下XML(示例):

但是,这不会输出任何内容。我尝试了以下XPath表达式:

  • //变体
  • //变式/文本()
  • //rsb:变体
  • //rsb:变体/文本()

正确的XPath表达式是什么?或者有没有更简单的方法来获取此元素?

我建议只需在文档中循环查找给定的标记

public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException, TransformerException {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("test.xml"));

    NodeList nodeList = document.getElementsByTagName("rsb:VersionInfo");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // do something with the current element
            System.out.println(node.getNodeName());
        }
    }
}
publicstaticvoidmain(字符串[]args)抛出SAXException、IOException、ParserConfiguration异常、TransformerException{
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
documentdocument=docBuilder.parse(新文件(“test.xml”);
NodeList NodeList=document.getElementsByTagName(“rsb:VersionInfo”);
for(int i=0;i
编辑:Yassin指出它不会得到子节点。这将为你找到孩子指明正确的方向

private static List<Node> getChildren(Node n)
  {
    List<Node> children = asList(n.getChildNodes());
    Iterator<Node> it = children.iterator();
    while (it.hasNext())
      if (it.next().getNodeType() != Node.ELEMENT_NODE)
        it.remove();
    return children;
  }
私有静态列表getChildren(节点n)
{
List childrends=asList(n.getChildNodes());
Iterator it=children.Iterator();
while(it.hasNext())
if(it.next().getNodeType()!=Node.ELEMENT\u Node)
it.remove();
返回儿童;
}

应该是rsb:VersionInfo吗?谢谢,@michael quatrani。您的回答非常有用,我已经为其他方法实现了您的机制。不过,我选择了另一条评论作为首选答案。@Robert没问题!很高兴你把它整理好了。XPathFactory需要使用名称空间上下文创建。一旦你算出了答案,
•//rsb:Variant
XPath应该可以工作。另外,请看这个问题。@JerryM的评论确实解决了我的问题。
public static void main(String[] args) throws SAXException, IOException,ParserConfigurationException, TransformerException {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("test.xml"));

    NodeList nodeList = document.getElementsByTagName("rsb:VersionInfo");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // do something with the current element
            System.out.println(node.getNodeName());
        }
    }
}
private static List<Node> getChildren(Node n)
  {
    List<Node> children = asList(n.getChildNodes());
    Iterator<Node> it = children.iterator();
    while (it.hasNext())
      if (it.next().getNodeType() != Node.ELEMENT_NODE)
        it.remove();
    return children;
  }