简体   繁体   English

从特定标签读取XML文件

[英]Read an XML File From Specific Tags

I am new to Read an XML File using Java! 我是第一次使用Java读取XML文件! Actually I want to read Paragraphs under a specific tag sample of XML is: 实际上,我想阅读XML的特定标记示例下的段落:

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

So i have an xml that contains paragraphs under the following tags 所以我有一个XML,其中包含以下标记下的段落

and i want to read the data inside these tags. 我想读取这些标签中的数据。 I have been trying this: 我一直在尝试:

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

The output I'm getting is not the complete one: 我得到的输出不是完整的输出:

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

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

my complete output should be a paragraph: 我的完整输出应为一个段落:

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. CARE管理总经理表示,他们开发开发商开发保险产品的能力有限,因此他们需要特别的专业知识。 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. 他们首先确定了整个流程的一系列基本要求:•了解保险原则和流程•小额信贷和小额信贷客户的经验•培训技能•对新产品开发过程的清晰愿景•精通营销和与之合作的能力并指导具有不同背景和动机的合作伙伴。

Can somebody help me please reading in the more specific way please thanks in advance! 有人可以帮助我,请以更具体的方式阅读,请提前谢谢!

You are looping through a node-set of ATTRIBUTE elements nList . 您正在遍历ATTRIBUTE元素nList的节点集。 So you get one of those elements which is in nNode , print it's name, test if it's an Element (it is), cast it to eElement and get it's attribute type successfully. 因此,您将获得nNode的那些元素nNode ,打印其名称,测试它是否为ElementElement ),将其转换为eElement并成功获取其属性type

But then you try to retrieve a node-set of elements named ATTRIBUTE in the context of the eElement : 但是,然后您尝试在eElement的上下文中检索名为ATTRIBUTE的元素的节点集:

eElement.getElementsByTagName("ATTRIBUTE")

which does not exist (your element contains just text - a whitespace character, actually), not a node-set of ATTRIBUTE elements. 哪个不存在(您的元素仅包含文本-实际上是一个空格字符),而不是ATTRIBUTE元素的节点集。

So then you have a null reference. 因此,您有一个空引用。 If you call any method on a null reference you get NullPointerException . 如果在空引用上调用任何方法,则会得到NullPointerException

What you probably want to do here is to get eElement.getNodeValue() which will give you the text contents of the tag. 您在这里可能想要做的是获取eElement.getNodeValue() ,它将为您提供标签的文本内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM