I have been working on this problem for quite some time and can’t figure it out. There is a given “xml” file that needs to be parsed and displayed on the screen:
<office>
<name>joe</name>
<surname>smith</surname>
<name>bob</name>
<surname>black</surname>
.....
</office>
I’ve found some great samples of codes on line but they don’t seem to work with an xml file that’s not set up correctly as this one, so if I’d add a tag I can get my code to work, but the problem is I can’t make any changes to the “xml” file.
It is someone else’s code I found here that’s been modified.
Here is my code with mods:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile{
public static void main (String argv []) throws ParserConfigurationException, SAXException, IOException{
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("office.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName() + "\n");
//counts how many times <name> is found in the file
//then the number is used in the for loop below
NodeList listOfTerms = doc.getElementsByTagName("name");
int totalTerms = listOfTerms.getLength();
System.out.println("Total no of terms : " + totalTerms + "\n");
for(int s= 0; s<listOfTerms.getLength() ; s++){
Node firstTermNode = listOfTerms.item(s);
if(firstTermNode.getNodeType() == Node.ELEMENT_NODE){
Element firstTermElement = (Element)firstTermNode;
//-------
NodeList firstWordList = firstTermElement.getElementsByTagName("name");
Element firstWordElement = (Element)firstWordList.item(0);
NodeList textWordList = firstWordElement.getChildNodes();
System.out.println("Name : " +
((Node)textWordList.item(0)).getNodeValue().trim());
//-------
NodeList defList = firstTermElement.getElementsByTagName("surname");
Element defElement = (Element)defList.item(0);
NodeList textDefList = defElement.getChildNodes();
System.out.println("Surname : " +
((Node)textDefList.item(0)).getNodeValue().trim());
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
The error message I get is this:
java.lang.NullPointerException
at Data.main(Data.java:45) //maybe a different line in the code for you.
If I use the root of the document for counter it prints the result once, for some reason getChildNodes() is not working correctly.
I notice that you do a .getElementsByTagName(“name”) twice. Are you expecting <name> tags within <name> ? If not then that is most likely the cause of your error, since the second time, it would return an empty list and will cause a NullPointerException when you try to reference firstWordElement
You can’t obtain the ‘surname’ from ‘name’ list which is what you are doing in the for loop. Get them in separate steps, so to fetch the ‘name’ elements:
and then to fetch the surname, just vary the tagname
Hope that helps.