srulison
asked on
Java application unable to read a Node value from an XML file.
Can somebody please help me out with this? I am trying to figure out why my java application is not returning the value of a Node from a Test.XML file that I set up. The code for this application is pretty basic and I have gone over it numerous times but the answer for fixing this is just not coming to me. Below is the java App, the XML file and the output. There are no run-time errors when I execute the program. It's simply not giving me all of the output that it's suppose to.
In the output, you will notice that the contents for the Node Test1 is blank. It should be Sample1. I cannot figure out why it is not returning the value.
Test.xml
In the output, you will notice that the contents for the Node Test1 is blank. It should be Sample1. I cannot figure out why it is not returning the value.
Test.xml
<Test>
<settings>
<Test1>sample1</Test1>
<Test2>Sample2</Test2>
</settings>
<settings>
<Test3>sample3</Test3>
<Test4>Sample4</Test4>
</settings>
</Test>
Java code:
/*
Note: This version of XML Reader has been modified to read a Test.XML file.
*/
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class XMLReader{
public static void main (String argv [])
{
try
{
//This basically this section just opens the XML file so it can be read and processed.
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("C:\\Users\\Steve\\JavaDev\\Test\\XML\\Test\\Test.xml"));
//Normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfSettings = doc.getElementsByTagName("settings");
int totalSettings = listOfSettings.getLength();
System.out.println("Total number of settings: " + totalSettings);
Node firstSettingsNode = listOfSettings.item(0);
if(firstSettingsNode.getNodeType() == Node.ELEMENT_NODE)
{
Element firstSettingsElement = (Element)firstSettingsNode;
//-------
NodeList test1List = firstSettingsElement.getElementsByTagName("Test1");
Element firstSettingslement = (Element)test1List.item(0);
NodeList textTest1List = firstSettingsElement.getChildNodes();
System.out.println("The contents of Test1 is " +
(( Node)textTest1List.item(0)).getNodeValue().trim());
}
}
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
}
Output:
M:\Test\XML\Test>java XMLReader
Root element of the doc is Test
Total number of settings: 2
The contents of Test1 is
M:\Test\XML\Test>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Exactly what I was looking for.
Not a problem, glad to help! :)
As for the code tags, I just edited your original question so that the Test.xml file, the Java code, and the resultant output were formatted as code blocks. It makes it easier to read anything of that nature, as it formats it in a fixed width font, and provides scroll bars to when there is a lot of content.
To do this just highlight the text/code that you want to format and then click the "Code" toolbar button in the editor toolbar just above where you type in comments (or questions). Here is an example of a code formatted block...
And here is the screenshot of me formatting it...
As for the code tags, I just edited your original question so that the Test.xml file, the Java code, and the resultant output were formatted as code blocks. It makes it easier to read anything of that nature, as it formats it in a fixed width font, and provides scroll bars to when there is a lot of content.
To do this just highlight the text/code that you want to format and then click the "Code" toolbar button in the editor toolbar just above where you type in comments (or questions). Here is an example of a code formatted block...
void sayHi() {
return "Hello World";
}
And here is the screenshot of me formatting it...
ASKER
In regards to your first comment about adding code tags, I'm not sure what you mean. Can you provide an example of a posting that has code tags so I can have that for future reference?
Thank you again for your help.