Results 1 to 9 of 9
- 02-14-2011, 07:23 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 32
- Rep Power
- 0
problem getting a piece of data out of XML
Greetings -
I have a very simple XML file - just a few lines and each line is a unique piece of data meaning no multiple addresses / names and so on -
I think in XML terms - no children
What I'm trying to do (as a learning deal) is (if I have my vocabulary correct) - parse the file and then pull out some data and print the data.
I assume this is not too complicated.
Before the code I have a Document doc = db.parse(file location)
Nodelist nodelst = doc.getElementByTagName("sample");
Here is the line I think is my working line:
When I open the file the data is there but the above code is not displaying the piece of data beside the tag.Java Code:NodeList sampledata = doc.getElementsByTagName("sample"); System.out.println("Data = " + sampledata);
Can you show me a little on dealing with a simple XML file?
What simple method can I acquire and display one piece of data within a parsed XML document?
Thanks for the help
Sparky
- 02-14-2011, 07:50 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
You are trying to print the NodeList object, that makes no sense!
What do you want is to print the Nodes from the List.
e.g.
Java Code:for (int i = 0; i < nodelst.getLength(); i++) { System.out.println(nodelst.item(i).getTextContent().trim()); }
- 02-14-2011, 07:55 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 32
- Rep Power
- 0
within my XML file is a few lines
<sample1>
<sample2>
and so on
is there a way w/o a loop to simply "pull" out 1 piece of data?
(More than likely I am misunderstanding how parsing and "getting" items works.)
I thought once I parsed it since I just have 1 piece of data I want I coudl simply get it w/o using a loop.
Thanks
Sparky
- 02-14-2011, 08:04 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
If you have only one element with the name "sample", then you will get a NodeList with one Node :) So you don`t need a loop then, you could use System.out.println(nodelst.item(0).getTextContent( ).trim()); e.g.
Or you can use XPath.
It would be helpful if you can give us a full sample xml file and informations about which tag/data do you want so I/we can give you an code example maybe :)
And are you using any external library such as jdom ?Last edited by eRaaaa; 02-14-2011 at 09:00 PM.
- 02-14-2011, 08:20 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 32
- Rep Power
- 0
eRaaaa - thanks for the help
Below is my sample XML file
I would like to just pick out certain pieces - real basic before I go back to the tutorials that have names and address for several people and use the loops and so forth.Java Code:<root> <sample0>A</sample0> <sample1>B</sample1> <sample2>C</sample2> <sample3>1</sample3> <sample4>2</sample4> <sample5>3</sample5> <sample6>4</sample6> </root>
I did try JDOM but gave up. I'm using:
Thanks again -Java Code:import java.net.URL; import java.net.*; import java.io.File; import org.w3c.dom.Document; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Element;
Sparky
- 02-14-2011, 08:53 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Mhm sorry, I think I can`t help you, I will repeat myself :)
I don`t understand your problem.
If you want to pick out the A of the Tag sample0:
or with childNodesJava Code://same DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.parse(new InputSource("YOURFILE.xml")); //getElementsByTag NodeList nodeLst = doc.getElementsByTagName("sample0"); System.out.println(nodeLst.item(0).getTextContent().trim());
or with XPathJava Code:System.out.println(doc.getFirstChild().getChildNodes().item(1).getTextContent());
or simple with jdomJava Code://xpath import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; //...... XPathFactory xfactory = XPathFactory.newInstance(); XPath path = xfactory.newXPath(); XPathExpression exp = path.compile("/root/sample0"); //sample0 System.out.println(exp.evaluate(doc));
all roads lead to Rome :DJava Code:public static void main(String[] args) throws Exception { Document doc = new SAXBuilder().build( "YOURFILE.xml" ); System.out.println(doc.getRootElement().getChildText("sample0")); }
- 02-14-2011, 09:15 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 32
- Rep Power
- 0
Well you did help!!!
Your code worked with what I wanted - I'm doing something stupid apparently. Inserting your code pulls out the data correctly - of course you knew that.
Can I ask a few questions -
- with NodeList - I should use (indexes) - since it's a list?
(this may explain why all of the examples and tutorials use a loop?)
I think I am confused (or was confused) with NodeList and the use of the index / subscript.
2) If I want to pick out more than 1 piece of data would it be:
NodeList nodeLst = doc.getElementsByTagName("sample0");
NodeList nodeLst2 = doc.getElementsByTagName("sample1");
NodeList nodeLst3 = doc.getElementsByTagName("sample2");
and so on?
Thanks
Sparky
- 02-14-2011, 09:25 PM #8
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Yes, the API Doc says:
* <p>The items in the <code>NodeList</code> are accessible via an integral
* index, starting from 0.
the reason of the loops are often because there are nodes with child nodes / or a list of nodes :)
yes and no :) it will work, but often there are better alternatives - your xml file is a little bit strange in my opinion ^^
2) If I want to pick out more than 1 piece of data would it be:
NodeList nodeLst = doc.getElementsByTagName("sample0");
NodeList nodeLst2 = doc.getElementsByTagName("sample1");
NodeList nodeLst3 = doc.getElementsByTagName("sample2");
and so on?
- 02-14-2011, 09:30 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 32
- Rep Power
- 0
ok
I can easily agree that my XML file is strange - chalk it up to a misunderstanding of the how's and why's of XML
My intent with this XML file was to start there and baby-step into the real uses.
You have been a big help, my questions are answered and I see where I should study and read next - I will keep reading and coding from here!!
Thanks so much!
Sparky
Similar Threads
-
Getting problem inserting data in data base
By anupama in forum New To JavaReplies: 4Last Post: 12-15-2010, 10:03 PM -
Determining whether a queen can attack another piece
By sonofshirt in forum New To JavaReplies: 9Last Post: 10-09-2010, 02:23 AM -
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
small piece of code: cannot set a max
By senca in forum New To JavaReplies: 1Last Post: 03-06-2010, 08:26 PM -
Decode this piece of Code
By mikeyl62 in forum New To JavaReplies: 2Last Post: 02-27-2010, 08:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks