Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML Paring with quotes in attribute value.

 
Kumar Navin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an XML as below to parse:
<LoadTicketRequest>
<LoadTicket LoadTicketID="S-123345">
.....
</LoadTicket>
</LoadTicketRequest>

But am not able to fetch the value of LoadTicketId attribute.

My code snippet is as below:
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("LoadTicket");
Element ticketelement = (Element) nodes.item(0);
NodeList ticketid = ticketelement.getElementsByTagName("LoadTicketID");
Element eticketid = (Element) ticketid.item(0);
System.out.println( "Ticket ID : " + getCharacterDataFromElement(eticketid) );

Can someone help me out as to where am going wrong.
 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the post title all about? There aren't any quotes in that attribute value.

As for why you can't get the value of the attribute, it's because you don't have any code which attempts to do that. You do have code which suggests that you think attributes are elements, though. They aren't. Have another look (or would that be "a" look) at the API document for Element to see how to get attribute values from an element.
 
Kumar Navin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. I was able to find the error. You are right, I was treating attribute as an element.
No more response needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic