如何使用Java中的DOM解析器仅获取xml中另一个节点内的节点

时间:2015-04-09 04:26:21

标签: java xml parsing dom selectnodes

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

这是我的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子节点,它应该从晚上标签获得。在每个晚上和早上的当前,它从食物项目中获取所有值,并再次获得所有值

1 个答案:

答案 0 :(得分: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());


             }


         }
     }