Results 1 to 5 of 5
  1. #1
    webdriver is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default cant read xml elements into list

    Here's the snipet. Every time it goes into the loop the elements are read but not added and there value is always null

    Java Code:
    public static List<Element> getChildrenByTagName() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        	
        	
        	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    	    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    	    Document doc = dBuilder.parse("Input.xml");
    	   
        	XPathFactory xpathFactory = XPathFactory.newInstance();
        	XPath xpath = xpathFactory.newXPath();
        	xpath.setNamespaceContext(new wdXMLNamespaceResolver(doc, false));
             //XPathExpression xPathQuery = xpath.compile(query);
             
        	Node links = (Node)xpath.evaluate("/userdata/David/tester[@id='1000']", doc, XPathConstants.NODE);
       
           List<Element> nodeList = new ArrayList<Element>();
           for (Node child = links.getFirstChild(); child != null; child = child.getNextSibling()) {
        	      if (child.getNodeType() == Node.ELEMENT_NODE ) {   	    	    	 
        	    	  nodeList.add((Element) child);
        	        
        	        
        	      }
    
    
    //Please help .. In a new role
    
        	}
         // System.out.println(nodeList.toString()); //Testing
          return nodeList;
        
        }
    Last edited by Fubarable; 08-09-2010 at 01:53 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Moderator edit: code tags added.

    I suggest you create a small compilable program and a data file that we can use so we can run and test your code for ourselves, increasing our chances of finding your error.

  3. #3
    webdriver is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    here a cleare compilable version


    package testSuites.consultantRegistration;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.xml.sax.SAXException;


    public class RunTest {

    /**
    * @param args
    */
    public static void main(String[] args) {


    try {
    getChildrenByTagName();
    } catch (XPathExpressionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }


    // TODO Auto-generated method stub
    public static void getChildrenByTagName() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {


    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse("Input.xml");

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    //XPathExpression xPathQuery = xpath.compile(query);

    Node links = (Node)xpath.evaluate("/userdata/David/tester[@id='1000']", doc, XPathConstants.NODE);

    List<Element> nodeList = new ArrayList<Element>();
    for (Node child = links.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child.getNodeType() == Node.ELEMENT_NODE ) {
    nodeList.add((Element) child);


    }
    }
    }
    }



    and XML file beloww


    <?xml version="1.0" encoding="utf-8"?>
    <userdata>
    <David>
    <tester id="1000">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <password>100000</password>
    <verifypassword>100000</verifypassword>
    <salary>100000</salary>
    <email> dsalami@hotmail.com</email>
    </tester>
    <tester id="1001">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1002">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1003">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1004">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1005">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1006">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1007">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1008">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1009">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1010">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1011">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1012">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1013">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1014">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    </David>
    <Arun>
    <tester id="1000">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <password>100000</password>
    <verifypassword>100000</verifypassword>
    <salary>100000</salary>
    <email> dsalami@hotmail.com</email>
    </tester>
    <tester id="1001">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1002">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1003">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1004">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1005">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1006">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1007">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1008">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1009">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1010">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1011">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1012">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1013">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    <tester id="1014">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
    </tester>
    </Arun>
    </userdata>

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by webdriver View Post
    here a cleare compilable version
    Great, but still, don't forget the code tags. I added it to your first post, and I'll let you add them to your code above. My signature will show you how, but quite simply you edit your post above and add the tag [cod&#101;] above any code block, and the tag [/cod&#101;] below any block.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
          List<Element> nodeList = new ArrayList<Element>();
          for (Node child = links.getFirstChild(); child != null; child = child.getNextSibling()) {
             if (child.getNodeType() == Node.ELEMENT_NODE) {
                nodeList.add((Element) child);
    
             }
          }
          for (Element element : nodeList) {
             System.out.printf("%s: %s%n", element.getNodeName(), element.getTextContent());
          }

Similar Threads

  1. how do i get elements from java beans from list?
    By anthrax in forum New To Java
    Replies: 5
    Last Post: 12-24-2009, 09:56 AM
  2. Replies: 27
    Last Post: 08-27-2009, 03:05 PM
  3. Capture elements from an List in a different package
    By blevault in forum New To Java
    Replies: 5
    Last Post: 11-27-2008, 09:14 AM
  4. Replies: 5
    Last Post: 10-17-2008, 02:13 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
  •