使用Java在文档中的任何位置定位XML元素

Rob*_*uch 5 java xml xpath

给出以下XML(示例):

<?xml version="1.0" encoding="UTF-8"?>
<rsb:VersionInfo xmlns:atom="http://www.w3.org/2005/Atom" xmlns:rsb="http://ws.rsb.de/v2">
    <rsb:Variant>Windows</rsb:Variant>
    <rsb:Version>10</rsb:Version>
</rsb:VersionInfo>
Run Code Online (Sandbox Code Playgroud)

我需要得到的价值VariantVersion.我目前的方法是使用XPath,因为我不能依赖于给定的结构.我所知道的是rsb:Version文档中有一个元素.

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//Variant";
InputSource inputSource = new InputSource("test.xml");
String result = (String) xpath.evaluate(expression, inputSource, XPathConstants.STRING);
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)

然而,这不输出任何东西.我尝试了以下XPath表达式:

  • //变
  • //变体/文本()
  • // RSB:变
  • // RSB:变体/文本()

什么是正确的XPath表达式?或者有更简单的方法来获得这个元素?

Mic*_*eue 3

我建议循环浏览文档来查找给定的标签

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:亚辛指出它不会获得子节点。这应该为您指明生孩子的正确方向。

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;
  }
Run Code Online (Sandbox Code Playgroud)