Reputation: 1093
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>
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.
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();
}
}
}
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.
How can I read the two <GivenName>
tags?
Upvotes: 1
Views: 12020
Reputation: 10473
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.
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());
}
Upvotes: 8