dom xml解析器java,相同的标签 [英] dom xml parser java, same tags

查看:137
本文介绍了dom xml解析器java,相同的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个xml文档具有不同数量的相同的命名标签。我如何得到子元素的数量及其值。

 <问题> 
< QuestionText> ABC?< / QuestionText>
<选项> A1 - XYZ< / Option>
<选项> A2 - WXY< / Option>
<选项> A2 - HJK< / Option>
< ID> 1< / ID>
< / Question>
<问题>
< QuestionText> ERY?< / QuestionText>
< QuestionText> NNN?< / QuestionText>
< QuestionText> KKKK?< / QuestionText>
< ID> 2< / ID>
< / Question>

输出应该读取...


ID:2有1个QuestionText和3个选项
QuestionText 1:ABC?选项1:A1 - XYZ
选项2:A2 - WXY选项3:A2 - HJK



ID:1有3个QuestionText和0个选项
QuestionText 1.ERY?
QuestionText 2.NNN?
QuestionText 3.KKKK?


我尝试过,但这会给出错误结果

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

在做了一些研究后,我找到了解决方案

 元素eElement =(Element)nNode; 

getTagValue(QuestionText,eElement);
getTagValue(选项,eElement);

private static void getTagValue(String sTag,Element eElement){
NodeList nlList = eElement.getElementsByTagName(sTag);
System.out.println(nodelist的大小:+ nlList.getLength()); (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());
}

}


解决方案

什么是 getTagValue()



无论如何,这是最好的教程(如何读取Java中的XML文件)为DOM中的DOM解析器在我的java。看看这个



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


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 - HJK

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

        }

解决方案

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

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

这篇关于dom xml解析器java,相同的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆