读取两个同名的 XML 标签 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22278326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading two XML tags with same name Java
提问by user1885868
I have an XML file to be read in Java, something like this:
我有一个要在 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>
In the beginning everything works fine, and then somthing like this shows up
一开始一切正常,然后出现这样的事情
<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>
As you can see, the <GivenName>
tag is mentioned twice in the same block, therefore, when I call the value from <GivenName>
it shows only the first one.
如您所见,该<GivenName>
标签在同一块中被提及两次,因此,当我调用<GivenName>
它的值时,只显示第一个。
This is the Java code that reads the XML file:
这是读取 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();
}
}
}
And this is the outcome:
这是结果:
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
As you can see, the second GivenName doesn't show up, and when I try to add a similar line to this one System.out.println("Given Name : " + eElement.getElementsByTagName("GivenName").item(0).getTextContent());
it gives me a NullPointer Exception
on the ones that don't have two Given names.
如您所见,第二个 GivenName 没有出现,当我尝试向该行添加类似的行时,System.out.println("Given Name : " + eElement.getElementsByTagName("GivenName").item(0).getTextContent());
它会NullPointer Exception
在没有两个GivenName 的行中显示一个。
How can I read the two <GivenName>
tags?
如何读取这两个<GivenName>
标签?
采纳答案by Marc Baumbach
The getElementsByTagName()
method on Element
will give you a NodeList
containing the matching child elements for the tag name provided. The documentation for NodeList
is here: http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html.
该getElementsByTagName()
方法Element
将为您提供一个NodeList
包含所提供标签名称的匹配子元素。文档在NodeList
这里:http: //docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html。
For an example of how to iterate over the GivenName
elements:
有关如何迭代GivenName
元素的示例:
NodeList giveNames = eElement.getElementsByTagName("GivenName");
for (int i = 0; i < givenNames.getLength(); i++) {
System.out.println("Given Name : " + givenNames.item(i).getTextContent());
}