我有一个用Java读取的XML文件,如下所示:
<Author AffiliationIDS="Aff1" PresentAffiliationID="Aff2">
<AuthorName DisplayOrder="Western">
<GivenName>Kun-Jing</GivenName>
<FamilyName>Lee</FamilyName>
</AuthorName>
</Author>
<Author AffiliationIDS="Aff1" PresentAffiliationID="Aff2">
<AuthorName DisplayOrder="Western">
<GivenName>John</GivenName>
<FamilyName>Smith</FamilyName>
</AuthorName>
</Author>
一开始一切正常,然后就会出现这样的事情
<Author AffiliationIDS="Aff1">
<AuthorName DisplayOrder="Western">
<GivenName>Z.</GivenName>
<GivenName>C.</GivenName>
<FamilyName>Huang</FamilyName>
</AuthorName>
</Author>
<Author AffiliationIDS="Aff1" PresentAffiliationID="Aff3">
<AuthorName DisplayOrder="Western">
<GivenName>J.</GivenName>
<GivenName>C.</GivenName>
<FamilyName>Chen</FamilyName>
</AuthorName>
</Author>
如您所见,<GivenName>
标记在同一个块中被提及两次,因此,当我从<GivenName>
调用该值时,它只显示第一个。
这是读取XML文件的Java代码:
package com.mkyong.seo;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class ReadXMLFile {
public static void main(String argv[]) {
try {
File fXmlFile = new File("/fileaddress/test-1.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("AuthorName");
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("Given Name : " + eElement.getElementsByTagName("GivenName").item(0).getTextContent());
System.out.println("Family Name : " + eElement.getElementsByTagName("FamilyName").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这就是结果:
Root element :AuthorGroup
----------------------------
Current Element :AuthorName
Given Name : Kun-Jing
Family Name : Lee
Current Element :AuthorName
Given Name : John
Family Name : Smith
Current Element :AuthorName
Given Name : Z.
Family Name : Huang
Current Element :AuthorName
Given Name : J.
Family Name : Chen
正如您所看到的,第二个GivenName没有显示,当我尝试向这个System.out.println("Given Name : " + eElement.getElementsByTagName("GivenName").item(0).getTextContent());
添加一个类似的行时,它给了我一个NullPointer Exception
的那个没有有两个名字。
如何阅读这两个<GivenName>
代码?
答案 0 :(得分:7)
getElementsByTagName()
上的Element
方法会为您提供NodeList
,其中包含所提供标记名称的匹配子元素。 NodeList
的文档位于:http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html。
有关如何迭代GivenName
元素的示例:
NodeList giveNames = eElement.getElementsByTagName("GivenName");
for (int i = 0; i < givenNames.getLength(); i++) {
System.out.println("Given Name : " + givenNames.item(i).getTextContent());
}