File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes XML Paring with quotes in attribute value. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Paring with quotes in attribute value." Watch "XML Paring with quotes in attribute value." New topic
Author

XML Paring with quotes in attribute value.

Kumar Navin
Ranch Hand

Joined: May 27, 2004
Posts: 52
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: May 27, 2004
Posts: 52
Thanks Paul. I was able to find the error. You are right, I was treating attribute as an element.
No more response needed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML Paring with quotes in attribute value.