Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dom xml解析器java,相同的标记_Java_Dom_Xml Parsing_Xmldom - Fatal编程技术网

dom xml解析器java,相同的标记

dom xml解析器java,相同的标记,java,dom,xml-parsing,xmldom,Java,Dom,Xml Parsing,Xmldom,我有一个xml文档,它有不同数量的相同命名标记。如何获得子元素的计数及其值 <Question> <QuestionText>ABC?</QuestionText> <Option>A1 - XYZ</Option> <Option>A2 - WXY</Option> <O

我有一个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>

美国广播公司?
A1-XYZ
A2-WXY
A2-HJK
1.
真的吗?
嗯?
KKK?
2.
输出应为

ID:2有1个问题文本和3个选项 问题1:ABC?选项1:A1-XYZ 选项2:A2-WXY选项3:A2-HJK

ID:1有3个问题文本和0选项 问题文本1.0? 问题文本2.NNN? 问题文本3.kkk

我试过了,但这会产生错误的结果

    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();    
}
元素eeelement=(元素)nNode;

对于(inti=0;i什么是
getTagValue()

无论如何,对我来说,这是java中DOM解析器的最佳教程()

下面是该链接中的
getTagValue()

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();    
}

@hilal…谢谢..嗯..我自己用过那个教程,我已经理解了它的工作原理..但我的问题是其他的。请看一下again@raqz如果它没有回答你的问题,你为什么要把它标记为答案?
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();    
}