当xml中有两个相似标签时获取XML的NodeValue

发布于 2024-11-23 19:34:39 字数 2892 浏览 1 评论 0原文

我正在使用 DOM 来解析 xml,但遇到了问题。在我的 xml 中,我有三个标签,即 strintstr。现在,在解析时,我为两个 str 标签获得了相同的值,但它们应该是不同的。

我的 XML

<result name="response" numFound="62996" start="0">
    <doc>
        <str name="body">
            a b c d e f g h i j k l m n o p q r s t u v w x  y z
        </str>
        <int name="content_id">123351</int>
        <str name="title">
            Alphabets
        </str>
    </doc>
</result>

xml 解析代码

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(gXml));
        Document doc = db.parse(is);
        NodeList infraline1 = doc.getElementsByTagName(node);

        sb.append("<results count=");
        sb.append("\"10\"");
        sb.append(">\r\n");

        for (int i = 0; i < infraline1.getLength(); i++) {
            Node node1 = infraline1.item(i);
            if (node1.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node1;
                NodeList nodelist = element.getElementsByTagName("str");
                Element element1 = (Element) nodelist.item(0);
                NodeList body = element1.getChildNodes();
                sb.append("<result>\r\n");
                sb.append("<body>");
                    sb.append((body.item(0)).getNodeValue().trim());
                sb.append("</body>\r\n");
                if (node1.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) node1;
                    NodeList nodelist1 = element2.getElementsByTagName("int");
                    Element element3 = (Element) nodelist1.item(0);
                    NodeList id = element3.getChildNodes();
                    sb.append("<id>");
                        sb.append(id.item(0).getNodeValue().trim());
                    sb.append("</id>\r\n");
                }
                if(node1.getNodeType() == Node.ELEMENT_NODE){
                    Element element4 = (Element) node1;
                    NodeList nodelist2 = element4.getElementsByTagName("str");
                    Element element5 = (Element) nodelist2.item(0);
                    NodeList title = element5.getChildNodes();
                    sb.append("<title>");
                        sb.append(title.item(0)).getNodeValue());
                    sb.append("</title>\r\n");
                }
                sb.append("</result>\r\n"); 
            }
        }
        sb.append("</results>");
    }

请帮忙,因为我需要为两个 str 节点获取不同的值

I'm using DOM to parse xml, but have run into an issue. In my xml i have three tags namely str, int and str. Now while parsing i get the same value for both the str tags whereas they should be different.

My XML

<result name="response" numFound="62996" start="0">
    <doc>
        <str name="body">
            a b c d e f g h i j k l m n o p q r s t u v w x  y z
        </str>
        <int name="content_id">123351</int>
        <str name="title">
            Alphabets
        </str>
    </doc>
</result>

Code for xml parsing

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(gXml));
        Document doc = db.parse(is);
        NodeList infraline1 = doc.getElementsByTagName(node);

        sb.append("<results count=");
        sb.append("\"10\"");
        sb.append(">\r\n");

        for (int i = 0; i < infraline1.getLength(); i++) {
            Node node1 = infraline1.item(i);
            if (node1.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node1;
                NodeList nodelist = element.getElementsByTagName("str");
                Element element1 = (Element) nodelist.item(0);
                NodeList body = element1.getChildNodes();
                sb.append("<result>\r\n");
                sb.append("<body>");
                    sb.append((body.item(0)).getNodeValue().trim());
                sb.append("</body>\r\n");
                if (node1.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) node1;
                    NodeList nodelist1 = element2.getElementsByTagName("int");
                    Element element3 = (Element) nodelist1.item(0);
                    NodeList id = element3.getChildNodes();
                    sb.append("<id>");
                        sb.append(id.item(0).getNodeValue().trim());
                    sb.append("</id>\r\n");
                }
                if(node1.getNodeType() == Node.ELEMENT_NODE){
                    Element element4 = (Element) node1;
                    NodeList nodelist2 = element4.getElementsByTagName("str");
                    Element element5 = (Element) nodelist2.item(0);
                    NodeList title = element5.getChildNodes();
                    sb.append("<title>");
                        sb.append(title.item(0)).getNodeValue());
                    sb.append("</title>\r\n");
                }
                sb.append("</result>\r\n"); 
            }
        }
        sb.append("</results>");
    }

Please help as i need to get different values for both str node

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

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

发布评论

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

评论(1

爱给你人给你 2024-11-30 19:34:39

我只看到声明

Element element1 = (Element) nodelist.item(0);

Element element3 = (Element) nodelist.item(0);

您确定您正在访问正确的元素吗?

...顺便说一句。代码真的很混乱,因为你同时解析和写入数据。尝试将解析后的 XML 放入自己的 ObjectMap 中,然后写入(格式化)它

I only see the statements

Element element1 = (Element) nodelist.item(0);

and

Element element3 = (Element) nodelist.item(0);

Are you sure, you are accesing the right elements.

... and by the way. The code is really confused, because you are parsing and writing data at once. Try to put the parsed XML into an own Object or Map and than write (format) it

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