Results 1 to 9 of 9
  1. #1
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default 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:
    Java Code:
     NodeList sampledata = doc.getElementsByTagName("sample");
    
                    System.out.println("Data = " + sampledata);
    When I open the file the data is there but the above code is not displaying the piece of data beside the tag.

    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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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());
    		}

  3. #3
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    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

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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 10:00 PM.

  5. #5
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    eRaaaa - thanks for the help

    Below is my sample XML file

    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 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.

    I did try JDOM but gave up. I'm using:
    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;
    Thanks again -
    Sparky

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    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:

    Java 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 childNodes
    Java Code:
    System.out.println(doc.getFirstChild().getChildNodes().item(1).getTextContent());
    or with XPath
    Java 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));
    or simple with jdom
    Java Code:
    	public static void main(String[] args) throws Exception {
    		Document doc = new SAXBuilder().build( "YOURFILE.xml" );
    		System.out.println(doc.getRootElement().getChildText("sample0"));
    	}
    all roads lead to Rome :D

  7. #7
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    Mhm sorry, I think I can`t help you, I will repeat myself :)
    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

  8. #8
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by Sparky View Post
    - 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.
    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 :)


    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?
    yes and no :) it will work, but often there are better alternatives - your xml file is a little bit strange in my opinion ^^

  9. #9
    Sparky is offline Member
    Join Date
    Jan 2011
    Posts
    32
    Rep Power
    0

    Default

    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

  1. Getting problem inserting data in data base
    By anupama in forum New To Java
    Replies: 4
    Last Post: 12-15-2010, 11:03 PM
  2. Determining whether a queen can attack another piece
    By sonofshirt in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 03:23 AM
  3. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  4. small piece of code: cannot set a max
    By senca in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 09:26 PM
  5. Decode this piece of Code
    By mikeyl62 in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 09:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •