JAVA XML - 如何在XML节点中获取特定元素? [英] JAVA XML - How do I get specific elements in an XML Node?

查看:97
本文介绍了JAVA XML - 如何在XML节点中获取特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个学术作业,我们给了一个非常大的XML文件,其中包含数百个条目。对于每个项目,我们应该列出经理的身份证件,最后一个人添加到列表中的人员的ID以及当前的项目数量。我已经阅读并重读了Oracle DOM API和各种Node API。我们正在使用JAVA,我无法为我的生活找出如何搜索每个 item_list 节点的各个fields。以下是我们给出的数据的一个例子。

This is an academic assignment and we are given an extremely large XML file with hundreds of entries like these. For each item we are supposed to list the Manager's ID, the Person's ID of the last person to add an item to the list, and the current number of items. I have read and reread the Oracle DOM API and various Node APIs. We are using JAVA and I cannot for the life of me figure out how to search various 'fields' of each item_list node. Below is an example of the data we are given.

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

我尝试过类似的操作:

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

    }

}

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

And messing around with this code for a while, but I would like to know how to retrieve data from various fields in each node.

推荐答案

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

If you are trying to read person attribute of manager tag, you can do it as shown below -

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

这篇关于JAVA XML - 如何在XML节点中获取特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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