迭代器不返回元素对象或不转换为其相应的对象?

发布于 2024-12-10 14:33:05 字数 737 浏览 5 评论 0原文

有人可以告诉我为什么这段代码中的迭代器不返回元素对象吗?!?无法投射到元素对象!这是 SAX 的 JDOM 实现!

        org.xml.sax.InputSource inStream = new org.xml.sax.InputSource();

        inStream.setCharacterStream(new java.io.StringReader(temp));


        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(inStream);   
        ArrayList<String> queries = new ArrayList<String>();
        Element root = doc.getRootElement();

        Iterator elemIter = root.getDescendants();

        while (elemIter.hasNext()) {

            **Element tempElem = (Element)elemIter.next();** 
            String CDATA = tempElem.getChildText("ZQuery");
            queries.add(CDATA);
            elemIter.next();
        }

Could someone please tell me why the Iterator in this code does not return with he Element Object?!? Can't cast to Element Object! This is a JDOM implementation of SAX!

        org.xml.sax.InputSource inStream = new org.xml.sax.InputSource();

        inStream.setCharacterStream(new java.io.StringReader(temp));


        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(inStream);   
        ArrayList<String> queries = new ArrayList<String>();
        Element root = doc.getRootElement();

        Iterator elemIter = root.getDescendants();

        while (elemIter.hasNext()) {

            **Element tempElem = (Element)elemIter.next();** 
            String CDATA = tempElem.getChildText("ZQuery");
            queries.add(CDATA);
            elemIter.next();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-12-17 14:33:05

考虑这个 XML 文档:

<root>
    <child/>
</root>

根的后代是:

  • 包含换行符的文本节点 + 4 个空格
  • 子元素
  • 包含换行符的文本节点

另外,getDescendants 遍历所有后代,而不是只是该元素的直接子元素。我很确定这不是你想要的。

Consider this XML document:

<root>
    <child/>
</root>

The descendants of the root are:

  • a text node containing the newline char + 4 spaces
  • the child element
  • a text node containing the newline char

Also, getDescendants walks through all the descendants, and not just the immediate children of the element. I'm pretty sure that it's not what you want.

听,心雨的声音 2024-12-17 14:33:05

您需要传递 ElementFiltergetDescentdents(Filter filter)

XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <child1>
        <child11></child11>
        <child12></child12>
    </child1>
    <child2></child2>
</root>

示例

 SAXBuilder builder = new SAXBuilder();
 Document document = builder.build(new File("src/com/foo/test.xml"));
 Element root = document.getRootElement();
 ElementFilter filter = new ElementFilter();
 Iterator i = root.getDescendants(filter);
 while (i.hasNext()) {
     Element element = (Element) i.next();
     System.out.println(element);
 }

输出

[Element: <child1/>]
[Element: <child11/>]
[Element: <child12/>]
[Element: <child2/>]

You need to pass an ElementFilter to getDescentdents(Filter filter)

XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <child1>
        <child11></child11>
        <child12></child12>
    </child1>
    <child2></child2>
</root>

Example

 SAXBuilder builder = new SAXBuilder();
 Document document = builder.build(new File("src/com/foo/test.xml"));
 Element root = document.getRootElement();
 ElementFilter filter = new ElementFilter();
 Iterator i = root.getDescendants(filter);
 while (i.hasNext()) {
     Element element = (Element) i.next();
     System.out.println(element);
 }

Output

[Element: <child1/>]
[Element: <child11/>]
[Element: <child12/>]
[Element: <child2/>]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文