Results 1 to 8 of 8
Thread: Operations on Nodes
- 02-05-2012, 02:38 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Operations on Nodes
Hi,
I am trying to learn how to read XML files in Java. I find it really difficult to grasp the structure of the DOM and how to operate on Nodes. I have read tons of tutorials but most provide just the code with litte or no explanation what is happening.
Here is the XML file I am trying to read:
And here is the code that supposed to output the firstnames of staff:Java Code:<?xml version="1.0"?> <company> <staff> <firstname>test</firstname> <lastname>test2</lastname> <nickname>test3</nickname> <salary>test4</salary> </staff> <staff> <firstname>test5</firstname> <lastname>test6</lastname> <nickname>test7</nickname> <salary>test8</salary> </staff> </company>
However, the output is just "null null".Java Code:import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.File; public class Test { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("/Users/Eleeist/Desktop/test.xml")); Element companyElement = document.getDocumentElement(); NodeList elements = companyElement.getElementsByTagName("firstname"); System.out.println(elements.item(0).getNodeValue() + " " + elements.item(1).getNodeValue()); } }
Could somebody please explain to me what is happening and what should I do to make it work? :)
Thanks.
- 02-05-2012, 03:02 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Operations on Nodes
Try to get the childnode first:
System.out.println(elements.item(0).getChildNodes( ).item(0).getNodeValue() + " " + elements.item(1).getChildNodes().item(0).getNodeVa lue());
or try to use getTextContent
System.out.println(elements.item(0).getTextContent () + " " + elements.item(1).getTextContent());
or use XPath :P
-
Re: Operations on Nodes
I think that a problem is that your XML has many more nodes than you realize. Everything is a Node. To see what I mean, recurse through your XML file and have it print out all of its contents (though I didn't display attributes since your simple file doesn't have any:
Java Code:import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.File; public class Xml001 { private static final String XML_FILE = "/Users/Eleeist/Desktop/test.xml"; // private static final String XML_FILE = "src/yr12/m02/a/Xml001.xml"; public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); File xmlFile = new File(XML_FILE); Document document = builder.parse(xmlFile); recurseThru(document, "", 0); // Element companyElement = document.getDocumentElement(); // NodeList elements = companyElement.getElementsByTagName("firstname"); // System.out.println(elements.item(0).getNodeValue() + " " // + elements.item(1).getNodeValue()); } private static void recurseThru(Node node, String tabs, int level) { System.out.println(tabs + "level: " + level); System.out.println(tabs + "node: " + node.getNodeName()); System.out.println(tabs + "value: " + node.getNodeValue()); System.out.println(tabs + "text: " + node.getTextContent()); if (node.hasChildNodes()) { tabs = "\t" + tabs; level++; NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); recurseThru(childNode, tabs, level); } } } }
- 02-05-2012, 03:27 PM #4
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Operations on Nodes
Indeed, I have not known that there were so many nodes. I tried aRaaaa advice and it works.
How would I go about reading elements attributes? Let's say the new XML file looks like this:
And here is the updated code:Java Code:<?xml version="1.0"?> <company> <staff> <firstname name="test" /> <lastname>test2</lastname> <nickname>test3</nickname> <salary>test4</salary> </staff> <staff> <firstname name="test5" /> <lastname>test6</lastname> <nickname>test7</nickname> <salary>test8</salary> </staff> </company>
Java Code:import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.File; public class Test { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("/Users/Eleeist/Desktop/test.xml")); Element companyElement = document.getDocumentElement(); NodeList elements = companyElement.getElementsByTagName("firstname"); System.out.println(elements.item(0).getTextContent() + " " + elements.item(1).getTextContent()); } }
-
Re: Operations on Nodes
- 02-05-2012, 03:45 PM #6
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Operations on Nodes
Got it:
Ok, the last thing: going down the levels.Java Code:import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.File; public class Test { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("/Users/Eleeist/Desktop/test.xml")); Element companyElement = document.getDocumentElement(); NodeList elements = companyElement.getElementsByTagName("firstname"); System.out.println(elements.item(0).getAttributes().item(0).getTextContent() + " " + elements.item(1).getAttributes().item(0).getTextContent()); } }
Let's say I have this:
How do I get to my "Hey" text?Java Code:<?xml version="1.0"?> <company> <staff> <deeper> <evendeeper> <somevalue>Hey!</somevalue> </evendeeper> </deeper> </staff> </company>
I tried something like this:
But I don't think it is the right thing to do (and it does not seem to work).Java Code:System.out.println(elements.item(0).getChildNodes().item(0).getChildNodes().item(0)...;
-
Re: Operations on Nodes
The simplest way to get that (that I know of) is to follow eRaaaaaaaaaaaa's (how many a's?) initial suggestion: use XPath. It's really the best answer to solve you problem.
This can use your DOM document so much of the code you have can be used with XPath.
- 02-05-2012, 04:43 PM #8
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Similar Threads
-
Operations on JList
By africanhacker in forum AWT / SwingReplies: 7Last Post: 03-21-2011, 04:15 PM -
Date operations
By SAR in forum New To JavaReplies: 9Last Post: 07-21-2010, 09:13 AM -
forcibly terminating io operations
By arnab321 in forum CLDC and MIDPReplies: 2Last Post: 11-15-2008, 08:51 PM -
Illegal Arithmetic Operations?
By Cruor in forum New To JavaReplies: 13Last Post: 09-19-2008, 04:46 PM -
String operations..
By sireesha in forum New To JavaReplies: 4Last Post: 12-14-2007, 02:04 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks