0

我必须获取节点内的节点值。但我最终从其他节点获得相同节点类型的值。

这是我的 xml 结构(示例)

    <Hello>
    <Morning>
    <hr>8</hr>
    <min>12</min>
    <sec>25</sec>
    <Food>
    <fooditem>
    <main>Burger</main>
    <drink>milk</drink>
    <fruit>banana</fruit>
    </fooditem>

    <fooditem>
    <main>Toast</main>
    <drink>butter milk</drink>
    <fruit>banana and apple</fruit>
    </fooditem>
    </Food>
    </Morning>
    <Evening>
 <hr>15</hr>
    <min>12</min>
    <sec>25</sec>
     <Food>
    <fooditem>
    <main>Burger</main>
    <drink>milk</drink>
    <fruit>banana</fruit>
    </fooditem>
 </Food>
    </Evening>
    <Evening>
<hr>15</hr>
    <min>12</min>
    <sec>25</sec>
     <Food>
    <fooditem>
    <main>Burger</main>
    <drink>milk</drink>
    <fruit>banana</fruit>
    </fooditem>
<fooditem>
    <main>Burger</main>
    <drink>milk</drink>
    <fruit>banana</fruit>
    </fooditem>
 </Food>
    </Evening>
    <Evening>
<hr>15</hr>
    <min>12</min>
    <sec>25</sec>
     <Food>
    <fooditem>
    <main>Burger</main>
    <drink>milk</drink>
    <fruit>banana</fruit>
    </fooditem>
 </Food>
    </Evening>

    </Hello>

现在的问题是我可以从 XML 中获取值。但我的代码所做的是它从所有其他节点获取所有食物项值。我必须依次获取早上的食物值,然后是晚上,然后是晚上。

我的代码

File fXmlFile = new File("c://food.xml");
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
     Document doc = dBuilder.parse(fXmlFile);
     doc.getDocumentElement().normalize();

     System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

     NodeList nList = doc.getElementsByTagName("fooditem");


     System.out.println("----------------------------");

     for (int temp = 0; temp < nList.getLength(); temp++) {

         Node nNode = nList .item(temp);


     if (nNode.getNodeType() == Node.ELEMENT_NODE) {

             Element eElement = (Element) nNode;
             System.out.println("main: " + eElement.getElementsByTagName("main").item(0).getTextContent());
             System.out.println("drink: " + eElement.getElementsByTagName("drink").item(0).getTextContent());
             System.out.println("fruit: " + eElement.getElementsByTagName("fruit").item(0).getTextContent());
         }
     }

输出:电流:

    Burger    
    milk    
    banana    Toast    
    butter milk    
    banana and apple    
    Burger    
    milk    
    banana    
    Burger    
    milk    
    banana    
    Burger    
    milk    
    banana 


    Burger    
    milk    
    banana    Toast    
    butter milk    
    banana and apple    
    Burger    
    milk    
    banana    
    Burger    
    milk    
    banana    
    Burger    
    milk    
    banana  



    Burger    
    milk    
    banana    Toast    
    butter milk    
    banana and apple    
    Burger    
    milk    
    banana    
    Burger    
    milk    
    banana    
    Burger    
    milk    
    banana

预期输出:

    Burger    
milk    
banana    
Toast    
butter
milk    
banana and apple    
Burger    
milk    
banana    
Burger    
milk    
banana    
Burger    
milk    
banana 

预期的输出是当它获得子节点时,它应该获得仅属于 Morning 标签的 fooditem 子节点,它应该从晚上标签中获得。在当前的每个晚上和早上,它从食物项中获取所有值,然后再次获取所有值

4

1 回答 1

0

尝试这个:

     System.out.println("----------------------------");
     NodeList nList = doc.getElementsByTagName("fooditem");

     for (int temp = 0; temp < nList.getLength(); temp++) {

         Node nNode = nList .item(temp);


     if (nNode.getNodeType() == Node.ELEMENT_NODE) {

             Element eElement = (Element) nNode;

             NodeList children = eElement.getChildNodes();

             for(int i=0;i<children.getLength();i++){
                 Node item = children.item(i);

                 System.out.println(item.getTextContent());


             }


         }
     }
于 2015-04-09T06:22:49.270 回答