File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Parsing of XML 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 "Parsing of XML" Watch "Parsing of XML" New topic

Parsing of XML

gurusamy sengodan
Ranch Hand

Joined: Oct 13, 2005
Posts: 41
Actually I need to parse the following XML.
I have to give "E2B_EXPORT" as input , i should get the id='1'.

String xmlResponseString = � <?xml version="1.0" encoding="UTF-8"?><usl><sch active='true' id='1' nam='E2B_EXPORT' sch-
description>ARIS DB FOR IMPORT
=</password><description>AGX DB FOR IMPORT EXPORT</description></trg></sch></usl>�;

while parsing I have to give "E2B_EXPORT" as input , i should get the id='1'.
How to do it. It's very urgent.

Thanks in Advance.
Dhanya Palanisamy
Ranch Hand

Joined: Oct 20, 2006
Posts: 61

First you have chhose a parser to parse the XML. Here is a small example.

DOMParser parser = new DOMParser();
Reader in = new BufferedReader(new FileReader("xml file "));
InputSource input = new org.xml.sax.InputSource(in);
Document doc = parser.getDocument();
Element rootElement= doc.getDocumentElement();
root = new DefaultMutableTreeNode(rootElement.getNodeName));

// root will have the root element of your XML. From that you can move down or compare and get your id using getAttribute("attribute_name") function

Hope this helps
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
make a DOM out of the xml.Using xPath get the node which has the name attribure as E2B_EXPORT.
Like //sch[@nam='E2B_EXPORT']
now query the node for id attribute using node.getAttribute("ID")

Rahul Bhattacharjee
LinkedIn - Blog
I agree. Here's the link:
subject: Parsing of XML
It's not a secret anymore!