dom xml 解析器 java,相同的标签
我有这个 xml 文档,其中有不同数量的相同命名标签。我怎样才能获得子元素的数量及其值。
<Question>
<QuestionText>ABC?</QuestionText>
<Option>A1 - XYZ</Option>
<Option>A2 - WXY</Option>
<Option>A2 - HJK</Option>
<ID>1</ID>
</Question>
<Question>
<QuestionText>ERY?</QuestionText>
<QuestionText>NNN?</QuestionText>
<QuestionText>KKKK?</QuestionText>
<ID>2</ID>
</Question>
输出应为...
ID:2 有 1 个问题文本和 3 个选项 问题文本1:ABC?选项 1:A1 - XYZ 选项 2:A2 - WXY 选项 3:A2 - HJK
ID:1 有 3 个问题文本和 0 个选项 问题文本 1.ERY? 问题文本 2.NNN? 问题文本 3.KKKK?
我尝试过,但这给出了错误结果
Element eElement = (Element) nNode;
for(int i=0;i<eElement.getChildNodes().getLength();i++){
System.out.println("NodeName:"+eElement.getNodeName());
System.out.println("Tag value:"+getTagValue("QuestionText",eElement));
System.out.println("Tag value:"+getTagValue("Option",eElement));
}
private static String getTagValue(String sTag, Element eElement){
NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
经过一些研究后,我找到了解决方案
Element eElement = (Element) nNode;
getTagValue("QuestionText",eElement);
getTagValue("Option",eElement);
private static void getTagValue(String sTag, Element eElement){
NodeList nlList = eElement.getElementsByTagName(sTag);
System.out.println("Size of nodelist:"+nlList.getLength());
for(int i=0;i<nlList.getLength();i++){
NodeList kList= eElement.getElementsByTagName(sTag).item(i).getChildNodes();
Node kValue = (Node) kList.item(0);
System.out.println("Node Value:"+kValue.getNodeValue());
}
}
i have this xml document which has varying number of same named tags. how can i get the count of the child elements and the value of it.
<Question>
<QuestionText>ABC?</QuestionText>
<Option>A1 - XYZ</Option>
<Option>A2 - WXY</Option>
<Option>A2 - HJK</Option>
<ID>1</ID>
</Question>
<Question>
<QuestionText>ERY?</QuestionText>
<QuestionText>NNN?</QuestionText>
<QuestionText>KKKK?</QuestionText>
<ID>2</ID>
</Question>
The output should read...
ID:2 Has 1 QuestionText and 3 Option
QuestionText 1:ABC? Option 1:A1 - XYZ
Option 2:A2 - WXY Option 3:A2 - HJKID:1 Has 3 QuestionText and 0 option
QuestionText 1.ERY?
QuestionText 2.NNN?
QuestionText 3.KKKK?
I tried, but this gives fault results
Element eElement = (Element) nNode;
for(int i=0;i<eElement.getChildNodes().getLength();i++){
System.out.println("NodeName:"+eElement.getNodeName());
System.out.println("Tag value:"+getTagValue("QuestionText",eElement));
System.out.println("Tag value:"+getTagValue("Option",eElement));
}
private static String getTagValue(String sTag, Element eElement){
NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
After doing some research I found the solution
Element eElement = (Element) nNode;
getTagValue("QuestionText",eElement);
getTagValue("Option",eElement);
private static void getTagValue(String sTag, Element eElement){
NodeList nlList = eElement.getElementsByTagName(sTag);
System.out.println("Size of nodelist:"+nlList.getLength());
for(int i=0;i<nlList.getLength();i++){
NodeList kList= eElement.getElementsByTagName(sTag).item(i).getChildNodes();
Node kValue = (Node) kList.item(0);
System.out.println("Node Value:"+kValue.getNodeValue());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getTagValue()
是什么?无论如何,这是最好的教程(对于我来说,如何在 Java 中读取 XML 文件以用于 Java 中的 DOM 解析器。看看这个
Here is
getTagValue()
来自该链接What is the
getTagValue()
?Anyway, it is the best tutorial (How to read XML file in Java) for DOM parser in java for me. Have a look at this
Here is
getTagValue()
from that link