读取两个具有相同名称Java的XML标记 [英] Reading two XML tags with same name Java

查看:226
本文介绍了读取两个具有相同名称Java的XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java读取的XML文件,如下所示:

I have an XML file to be read in Java, something like this:

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

如您所见,< GivenName> tag在同一个块中被提及两次,因此,当我从< GivenName> 调用该值时,它只显示第一个。

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.

这是读取XML文件的Java代码:

This is the Java code that reads the XML file:

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异常那些没有两个名字的人。

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> 标签?

How can I read the two <GivenName> tags?

推荐答案

元素上的 getElementsByTagName()方法将为您提供 NodeList 包含所提供标记名称的匹配子元素。 NodeList 的文档在这里: http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html

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.

有关如何迭代 GivenName 元素的示例:

For an example of how to iterate over the GivenName elements:

NodeList giveNames = eElement.getElementsByTagName("GivenName");
for (int i = 0; i < givenNames.getLength(); i++) {
    System.out.println("Given Name : " + givenNames.item(i).getTextContent());
}

这篇关于读取两个具有相同名称Java的XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆