Java XML解析器以返回同一级别的所有标签

huangapple go评论82阅读模式
英文:

Java XML Parser to return all tags at the same level

问题

你可以尝试使用XPath表达式 //document/tag 来获取直接嵌套在 <document> 内的 <tag> 元素。这个表达式只会匹配位于直接子级位置的 <tag> 元素,而不会匹配子元素中的 <tag> 元素。

以下是你的代码的修改版本:

public static void parseXML() {
    File file = new File("src//data//data.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;

    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    Document doc = null;

    try {
        doc = documentBuilder.parse(file);
    } catch (SAXException | IOException e) {
        e.printStackTrace();
    }

    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        XPathExpression expr = xPath.compile("//document/tag"); // 修改XPath表达式

        NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.println(i);
            System.out.println(nodeList.item(i).getTextContent());
        }

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
}

通过这个修改,XPath表达式 //document/tag 只会返回直接嵌套在 <document> 内的 <tag> 元素。

英文:

TLDR of what I'm trying to do:

  • Use an xml document to store representation of classes.

  • Use some parser class to recurse through the file and instantiate instances of those classes.

  • I am currently having issues with getting child elements (and not grandchildren) of the nodeList.

Thanks in Advance.

Suppose I have the following xml document

====== data.xml ======
&lt;document&gt;
    &lt;tag&gt;
        &lt;child1&gt; some content here &lt;/child1&gt;
        &lt;child2&gt; some other content &lt;/child2&gt;
    &lt;/tag&gt;

    &lt;tag&gt;
        &lt;child1&gt; interesting content here &lt;/child1&gt;
        &lt;child2&gt;
            &lt;tag&gt;
                &lt;child1&gt; a b c d &lt;/child1&gt;
                &lt;child2&gt; e f g h &lt;/child2&gt;
            &lt;/tag&gt;
        &lt;/child2&gt;
    &lt;/tag&gt;
&lt;/document&gt;

I have tried using the xFile parser to get all tag nodes. However, when I do the following:

public static void parseXML() {
    File file = new File(&quot;src//data//data.xml&quot;);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;

    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    Document doc = null;

    try {
        doc = documentBuilder.parse(file);
    } catch (SAXException | IOException e) {
        e.printStackTrace();
    }

    try {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();
        XPathExpression expr = xPath.compile(&quot;//document//tag&quot;);

        NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

        for (int i = 0; i &lt; nodeList.getLength(); i++) {
            System.out.println(i);
            System.out.println(nodeList.item(i).getTextContent());
        }

    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
}

I get the following output:

0

         some content here 
         some other content 
    
1

         interesting content here 
        
            
                 a b c d 
                 e f g h 
            
        
    
2

                 a b c d 
                 e f g h 

How do I get it to ONLY return the &lt;tag&gt; elements which are directly nested in &lt;document&gt; (because the path provided to xPath.compile(String string); is //document//tag

答案1

得分: 1

为了回答你提出的具体问题 - 我会将这部分代码更改为

XPathExpression expr = xPath.compile("/document/tag[1]/*");

// 匹配所有子节点,根据你的结构,你没有任何子节点。

英文:

To answer the specific question you have posed - I would change this

    XPathExpression expr = xPath.compile(&quot;//document//tag&quot;);

to this

    XPathExpression expr = xPath.compile(&quot;/document/tag[1]/*&quot;);

The // matches all sub nodes and according to your structure you don't have any.

huangapple
  • 本文由 发表于 2020年7月22日 12:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63026983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定