JAVA XML - 如何在XML节点中获取特定元素?

boo*_*box 5 java xml dom

这是一项学术任务,我们获得了一个非常大的XML文件,其中包含数百个这样的条目.对于每个项目,我们应该列出经理的ID,将项目添加到列表的最后一个人的人员ID,以及当前的项目数.我已阅读并重读了Oracle DOM API和各种Node API.我们正在使用JAVA,我不能为我的生活弄清楚如何搜索每个item_list节点的各个"字段" .以下是我们给出的数据示例.

<item_list id="item_list01">
   <numitems_intial>5</numitems_initial>
   <item>
     <date_added>1/1/2014</date_added>
     <added_by person="person01" />
   </item>
   <item>
      <date_added>1/6/2014</date_added>
      <added_by person="person05" />
    </item>
    <numitems_current>7</numitems_current>
    <manager person="person48" />
</item_list>
<item_list id="item_list02">
   <numitems_intial>5</numitems_initial>
   <item>
     <date_added>1/15/2014</date_added>
     <added_by person="person05" />
   </item>
   <item>
     <date_added>1/1/2014</date_added>
     <added_by person="person09" />
   </item>
       <item>
         <date_added>1/9/2014</date_added>
         <added_by person="person45" />
       </item>
        <numitems_current>7</numitems_current>
        <manager person="person38" />
    </item_list>
Run Code Online (Sandbox Code Playgroud)

我尝试过做类似的事情:

NodeList nodes = queryDoc.getElementsByTagName("item_list");

for(int i = 0; i < nodes.getLength(); i++) {

    Node node = nodes.item(i);

    if(node != null) {

       System.out.println(node.manager);

    }

}
Run Code Online (Sandbox Code Playgroud)

并乱用这段代码一段时间,但我想知道如何从每个节点的各个字段中检索数据.

Kar*_*tic 11

如果您正在尝试读取经理标签的人员属性,您可以按如下所示进行操作 -

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Test{

public static void main (String[] args)
{
    Test test = new Test();
    test.readXML();
}

private void readXML()
{
    Document doc = null;
    try 
    {
        doc = parseXML("/home/abc/Test.xml");
    } 
    catch (ParserConfigurationException e) 
    {
        e.printStackTrace();
    } 
    catch (SAXException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    if(doc != null)
    {
        NodeList nList = doc.getElementsByTagName("item_list");
        for (int i = 0; i < nList.getLength(); i++) 
        {
            Node nNode = nList.item(i);
            Element eElement = (Element) nNode;
            Element cElement =  (Element) eElement.getElementsByTagName("manager").item(0);
            System.out.println("Manager ID : " + cElement.getAttribute("person"));
        }
    }
}

private Document parseXML(String filePath) throws ParserConfigurationException, SAXException, IOException
{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(filePath);
    doc.getDocumentElement().normalize();
    return doc;
}
}
Run Code Online (Sandbox Code Playgroud)