解析 Java 的 XML。

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

parsing Xml for Java

问题

以下是翻译好的部分:

存在这样的XML文档:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <root>
  3. <a/>
  4. <a/>
  5. <a/>
  6. <b>
  7. <c/>
  8. <d/>
  9. <c/>
  10. <d/>
  11. <c/>
  12. <d/>
  13. </b>
  14. </root>

以下代码用于获取 "a" 节点的NodeList:

  1. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  2. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  3. Document doc = dBuilder.parse(xmlfile);
  4. doc.getDocumentElement().normalize();
  5. NodeList aNodes = doc.getElementsByTagName("a");

但是,在Java中如何检索 "c" 节点的NodeList呢?
我尝试获取 "b" 节点的子节点,但是没有结果,getLength() = 0。

  1. NodeList bNodes = doc.getElementsByTagName("b");
  2. NodeList cNodes = bNodes.item(0).getChildNodes();
英文:

There exists XML document like this:
<?xml version="1.0" encoding="utf-8"?>

  1. &lt;root&gt;
  2. &lt;a/&gt;
  3. &lt;a/&gt;
  4. &lt;a/&gt;
  5. &lt;b&gt;
  6. &lt;c/&gt;
  7. &lt;d/&gt;
  8. &lt;c/&gt;
  9. &lt;d/&gt;
  10. &lt;c/&gt;
  11. &lt;d/&gt;
  12. &lt;/b&gt;
  13. &lt;/root&gt;

Following code is using to get "a" NodeList:

  1. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  2. DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  3. Document doc = dBuilder.parse(xmlfile);
  4. doc.getDocumentElement().normalize();
  5. NodeList aNodes = doc.getElementsByTagName(&quot;a&quot;);

But how in Java I can retrieve NodeList of "c"?
I tried to get nodes of "b" nodes but it have not results, getlength() = 0.

  1. NodeList bNodes = doc.getElementsByTagName(&quot;b&quot;);
  2. NodeList cNodes = bNodes.item(0).getChildNodes();

答案1

得分: 1

你可以像这样使用 doc.getElementsByTagName(&quot;c&quot;) 来获取它们。

  1. NodeList cNodes = doc.getElementsByTagName(&quot;c&quot;);
  2. for (int i = 0; i &lt; cNodes.getLength(); ++i)
  3. System.out.println(cNodes.item(i));

输出:

  1. [c: null]
  2. [c: null]
  3. [c: null]

我也可以检索到 "b" 的 NodeList。

  1. NodeList bNodes = doc.getElementsByTagName(&quot;b&quot;);
  2. NodeList bChildren = bNodes.item(0).getChildNodes();
  3. for (int i = 0; i &lt; bChildren.getLength(); ++i)
  4. System.out.println(bChildren.item(i));

输出:

  1. [#text:
  2. ]
  3. [c: null]
  4. [#text:
  5. ]
  6. [d: null]
  7. [#text:
  8. ]
  9. [c: null]
  10. [#text:
  11. ]
  12. [d: null]
  13. [#text:
  14. ]
  15. [c: null]
  16. [#text:
  17. ]
  18. [d: null]
  19. [#text:
  20. ]
英文:

You can get them by doc.getElementsByTagName(&quot;c&quot;) like this.

  1. NodeList cNodes = doc.getElementsByTagName(&quot;c&quot;);
  2. for (int i = 0; i &lt; cNodes.getLength(); ++i)
  3. System.out.println(cNodes.item(i));

output:

  1. [c: null]
  2. [c: null]
  3. [c: null]

I can also retrieve NodeList of "b".

  1. NodeList bNodes = doc.getElementsByTagName(&quot;b&quot;);
  2. NodeList bChildren = bNodes.item(0).getChildNodes();
  3. for (int i = 0; i &lt; bChildren.getLength(); ++i)
  4. System.out.println(bChildren.item(i));

output:

  1. [#text:
  2. ]
  3. [c: null]
  4. [#text:
  5. ]
  6. [d: null]
  7. [#text:
  8. ]
  9. [c: null]
  10. [#text:
  11. ]
  12. [d: null]
  13. [#text:
  14. ]
  15. [c: null]
  16. [#text:
  17. ]
  18. [d: null]
  19. [#text:
  20. ]

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

发表评论

匿名网友

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

确定