当xml中有两个相似标签时获取XML的NodeValue
我正在使用 DOM 来解析 xml,但遇到了问题。在我的 xml 中,我有三个标签,即 str、int 和 str。现在,在解析时,我为两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只看到声明
和
您确定您正在访问正确的元素吗?
...顺便说一句。代码真的很混乱,因为你同时解析和写入数据。尝试将解析后的 XML 放入自己的
Object
或Map
中,然后写入(格式化)它I only see the statements
and
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
orMap
and than write (format) it