Results 1 to 2 of 2
Thread: Parsing XML attributes
- 11-02-2010, 12:25 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
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:
I'm using the following but no data is being returned (as a result, I get the java.lang.NullPointerException)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>
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.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(); } }
- 11-02-2010, 12:39 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you read about other Node types? Read them first of all if you can. There are few.
Similar Threads
-
Get CSS Attributes using Java
By justinmifsud in forum Advanced JavaReplies: 0Last Post: 09-20-2010, 04:11 PM -
execute a .exe with attributes from java
By danielleon11 in forum Advanced JavaReplies: 1Last Post: 07-14-2010, 01:25 AM -
Problem with static attributes
By AndreRodrigues in forum New To JavaReplies: 5Last Post: 01-03-2010, 07:31 AM -
The PrintRequestAttributeSet is not accepting the given attributes.
By Iyyam in forum New To JavaReplies: 0Last Post: 09-10-2009, 12:35 PM -
How to read AIX attributes & values.
By John_28 in forum New To JavaReplies: 5Last Post: 05-05-2008, 08:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks