gpt4 book ai didi

java - 读取两个同名 Java 的 XML 标签

转载 作者:行者123 更新时间:2023-11-29 06:35:09 25 4
gpt4 key购买 nike

我有一个要用 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>标记在同一个 block 中被提及两次,因此,当我调用来自 <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>标签?

最佳答案

Element 上的 getElementsByTagName() 方法将为您提供一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com