Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Unhappy Parsing XML attributes

    I just read the XML tutorial and I understand that I'm parsing XML that contains attributes. However, the sample parser examples only seem to parse XML containing elements and I'm not sure how to parse out the attributes. For example, given the following XML:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <group size="4" name="zipcode">
      <user login="user1"/>
      <user login="user2"/>
      <user login="user3"/>
      <user login="user4"/>
      <summary>
        <!-- Total saved this time unit -->
        <fulfilledSavings>
          <impact type="dollar" name="dollars" amount="-169.64"/>
          <impact type="miles" name="number of miles" amount="286.61"/>
        </fulfilledSavings>
      </summary>
    </group>
    I'm using the following but no data is being returned (as a result, I get the java.lang.NullPointerException)

    Java Code:
    public XMLReader(String xmlToParse) {
    		try {
    			  
    			// http://forums.sun.com/thread.jspa?threadID=513942
    			
    			  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    			  DocumentBuilder db = dbf.newDocumentBuilder();
    			  
    			  InputSource inStream = new InputSource();
    			  inStream.setCharacterStream(new StringReader(xmlToParse));
    			  
    			  Document doc = db.parse(inStream);
    			  
    			  System.out.println("Root element " + doc.getDocumentElement().getNodeName());
    			  NodeList nodeLst = doc.getElementsByTagName("fulfilledSavings");
    			  System.out.println("Information of all summary");
    
    			  for (int s = 0; s < nodeLst.getLength(); s++) {
    
    			    Node fstNode = nodeLst.item(s);
    			    
    			    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
    			  
    			      Element fstElmnt = (Element) fstNode;
    			      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("fulfilledSavings");
    			      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    			      NodeList fstNm = fstNmElmnt.getChildNodes();
    			      System.out.println("fulfilledSavings: "  + ((Node) fstNm.item(0)).getNodeValue());
    			      //NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
    			      //Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
    			      //NodeList lstNm = lstNmElmnt.getChildNodes();
    			      //System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
    			    }
    
    			  }
    			  } catch (Exception e) {
    			    e.printStackTrace();
    			  }
    		}
    Could someone let me know if there's another type of parser I should be using and/or if I'm missing something simple. Thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Get CSS Attributes using Java
    By justinmifsud in forum Advanced Java
    Replies: 0
    Last Post: 09-20-2010, 05:11 PM
  2. execute a .exe with attributes from java
    By danielleon11 in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2010, 02:25 AM
  3. Problem with static attributes
    By AndreRodrigues in forum New To Java
    Replies: 5
    Last Post: 01-03-2010, 08:31 AM
  4. Replies: 0
    Last Post: 09-10-2009, 01:35 PM
  5. How to read AIX attributes & values.
    By John_28 in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 09:07 AM

Tags for this Thread

Posting Permissions

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