我有一个要用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());
行添加相似的行时,对于没有两个Given名字的行,它会给我一个NullPointer Exception
。如何阅读两个
<GivenName>
标签? 最佳答案
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());
}
关于java - 读取两个具有相同名称的Java的XML标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22278326/