本文介绍了使用DOM迭代包含一些具有相同名称的标签的NodeList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?xml version =1.0? > 
< record>
< user>
< name> Leo< / name>
< email> **** @ ****。com< / email>
< food-list>
< food>汉堡< / food>
< food>鱼< / food>
< / food-list>
< / user>
< / record>

我目前的解决方案是

 $ code> for(int userNumber = 0; userNumber< masterList.getLength(); userNumber ++){

Node singleUserEntry = masterList.item(userNumber);
if(singleUserEntry.getNodeType()== Node.ELEMENT_NODE){

org.w3c.dom.Element userEntryElement =(org.w3c.dom.Element)singleUserEntry;



System.out.println(name:+ getTagValue(name,userEntryElement));
System.out.println(email:+ getTagValue(email,userEntryElement));
NodeList foodList = userEntryElement.getElementsByTagName(food-list)。item(0).getChildNodes(); (int i = 0; i< foodList.getLength(); i ++){
节点foodNode = foodList.item(i);

System.out.println(food:+ foodNode.getNodeValue());
}

private static String getTagValue(String sTag,org.w3c.dom.Element eElement){
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)。 getChildNodes();
Node nValue =(Node)nlList.item(0);
return nValue.getNodeValue();

现在的输出是

 名称:Leo 
电子邮件:******@*****.com
食物:
食物:null
食物:
食物:null
食物:

这让我很困惑。你能告诉我我错了什么吗?食物标签的数量不是预先定义的。

解决方案
 ((Node)foodNode .getChildNodes()。item(0))。getNodeValue()

请注意,看到,在Java中处理DOM API是相当痛苦的。您是否查看了或?


I'm trying to read in an XML using DOM in Java

<?xml version="1.0"?>
<record>
<user>
    <name>Leo</name>
    <email>****@****.com</email>
        <food-list>
            <food>Hamburgers</food>
            <food>Fish</food>
        </food-list>
</user>
</record>

My current solution is

    for (int userNumber = 0; userNumber < masterList.getLength(); userNumber++) {

           Node singleUserEntry = masterList.item(userNumber);
           if (singleUserEntry.getNodeType() == Node.ELEMENT_NODE) {

              org.w3c.dom.Element userEntryElement = (org.w3c.dom.Element) singleUserEntry;



              System.out.println("name : " + getTagValue("name", userEntryElement));
              System.out.println("email : " +getTagValue("email", userEntryElement));
              NodeList foodList = userEntryElement.getElementsByTagName("food-list").item(0).getChildNodes();
              for(int i = 0; i < foodList.getLength(); i++){
                  Node foodNode = foodList.item(i);
                  System.out.println("food : " + foodNode.getNodeValue());
              }

private static String getTagValue(String sTag, org.w3c.dom.Element eElement) {
     NodeList nlList =  eElement.getElementsByTagName(sTag).item(0).getChildNodes();
     Node nValue = (Node) nlList.item(0);
     return nValue.getNodeValue();

And the output now is

name : Leo 
email : ******@*****.com
food :          
food : null
food :          
food : null
food : 

Which quite confuses me. Could you tell me where I'm wrong? The number of food tags is not pre-defined.

解决方案
((Node) foodNode.getChildNodes().item(0)).getNodeValue()

Note that, as you can clearly see, dealing with the DOM API in Java is pretty painful. Have you looked at JDOM or dom4j?

这篇关于使用DOM迭代包含一些具有相同名称的标签的NodeList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:56