从特定标记读取XML文件

时间:2014-03-12 17:36:22

标签: java xml tags

我是使用Java读取XML文件的新手!实际上我想读取XML的特定标签样本下的Paragraphs:

  <ATTRIBUTE name="Documentation" type="STRING"> </ATTRIBUTE>

  <ATTRIBUTE name="__Variants__" type="LONGSTRING"> </ATTRIBUTE>

  <ATTRIBUTE name="GUID_NPI" type="STRING"> </ATTRIBUTE>

  <ATTRIBUTE name="Status" type="ENUMERATION">None</ATTRIBUTE>

  <ATTRIBUTE name="Order" type="INTEGER">0</ATTRIBUTE>

  <ATTRIBUTE name="Costs" type="DOUBLE">0</ATTRIBUTE>

  <ATTRIBUTE name="Description" type="STRING">CARE management manage manager managing recognised that their capacity to develop developer development developing an insurance       product was limited and they would need to bring in particular expertise. They first       identified an array of basic requirements for the process as a whole: • Understanding of       insurance principles and processes • Experience of microfinance and microfinance clients •       Training skills • Clear vision of the new product development process • Marketing savvy, and       Ability to work with and guide partners that have different backgrounds and motivations.      </ATTRIBUTE>

  <ATTRIBUTE name="Comment" type="STRING"> </ATTRIBUTE>

  <ATTRIBUTE name="External documentation" type="PROGRAMCALL">ITEM "" param:""      </ATTRIBUTE>

  <INTERREF name="Organizational unit"> </INTERREF>

  <ATTRIBUTE name="Info on results" type="STRING"> </ATTRIBUTE>

所以我有一个xml,其中包含以下标记下的段落

我想读取这些标签内的数据。我一直在尝试这个:

 try {

File fXmlFile = new File("c:\\Data.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("ATTRIBUTE");

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

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

    Node nNode = nList.item(temp);

    System.out.println("\nCurrent Element :" + nNode.getNodeName());

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

        Element eElement = (Element) nNode;

        System.out.println("DATA : "+temp+"  " + eElement.getAttribute("type"));
        System.out.println("PARAGRAPH : " + eElement.getElementsByTagName("ATTRIBUTE").item(0).getTextContent());

    }
}
} catch (Exception e) {
e.printStackTrace();
}

我得到的输出不完整:

Root element :ADOXML
----------------------------

 Current Element :ATTRIBUTE
 Staff id : STRING
 java.lang.NullPointerException
at javaapplication5.JavaApplication5.main(JavaApplication5.java:53)

我的完整输出应该是段落:

CARE管理层管理经理认识到他们开发开发保险产品的开发人员开发的能力有限,他们需要带来特定的专业知识。他们首先确定了整个过程的一系列基本要求:•了解保险原则和流程•小额信贷和小额信贷客户的经验•培训技能•新产品开发过程的清晰愿景•营销头脑和工作能力并指导具有不同背景和动机的合作伙伴。

有人可以帮助我,请以更具体的方式阅读,请提前致谢!

1 个答案:

答案 0 :(得分:0)

您正在循环遍历ATTRIBUTE个元素nList的节点集。所以你得到nNode中的其中一个元素,打印它的名字,测试它是Element(它是),把它投到eElement并成功获取它的属性type

但是,您尝试在ATTRIBUTE的上下文中检索名为eElement的元素的节点集:

eElement.getElementsByTagName("ATTRIBUTE")

不存在(您的元素只包含文本 - 实际上是空格字符),而不是ATTRIBUTE元素的节点集。

那么你有一个空引用。如果您在空引用上调用任何方法,则会得到NullPointerException

你可能想要做的是获得eElement.getNodeValue(),它将为你提供标签的文字内容。