Web Development

Xml parsing using sun parser with servlet

Xml parsing using sun parser with servlet are given below:

Import following packages:

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;

Then parsing your xml data which is given by parameter:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String xmlRequest = new String(request.getParameter("xml")); // get parameter
Document doc = db.parse(new InputSource(new StringReader(xmlRequest)));
NodeList nodeLst = doc.getElementsByTagName("clientUpdate");
Node fstNode = nodeLst.item(0);
Element fstElmnt = (Element) fstNode;

String clcref="";
try{
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("CL.CREF");
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    NodeList fstNm = fstNmElmnt.getChildNodes();
    clcref=((Node) fstNm.item(0)).getNodeValue();
}catch(Exception e ){
    e.printStackTrace();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s