Wednesday, August 3, 2011

How to Parse XML Documents using JDOM


JDOM : Alternative of DOM and SAX.

DOM : parse xml document and load it into memory.  Too slow and consume more memory .
SAX  : it does'nt load xml document in to memory. instead it create object representation of xml document.
too fast and , efficient and

JDOM : Efficient reading, manipulation, and writing. This is lightweight api , fast and well optimized for java programmer . apart from that it will integrate well with both DOM and SAX.




Note : JDOM.jar  API should be used to work with these examples.


Example ! :



<xml>
<response>
<status-code>0</status-code>
<status-text>ACCEPTED</status-text>
<tid>131113893927817642</tid>
</response>
</xml>


 Requirement : Parsing XML using JDOM  Example 1  



public String parsingXMLUsingJDOM(String strXml) throws Exception {
        String xmlResponse = null;
        SAXBuilder builder = new SAXBuilder();
        InputStream is = new ByteArrayInputStream(strXml.getBytes());


        try {


            Document document = (Document) builder.build(is);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren("response");
            for (int i = 0; i < list.size(); i++) {
            Element node = (Element) list.get(i);
            xmlResponse = node.getChildText("status-code");
            
            }
        } catch (IOException io) {
            System.out.println(io.getMessage());
        }  finally {
              if (builder != null) {
                builder = null;
             }
             if (is != null) {
                try {




                    is.close();
                    is = null;
                } catch (Exception e) {
                    e.printStackTrace();
                    ;
                }
            }
        }


        return xmlResponse;
    }




Note : In order to retrieve all the child nodes in the above XML Format , use concat() method .


Example 2 : Parsing XML Document using JDOM Example 2


XML Format :


<xml>
<x_extraparams>name=dasari , empid=1786</x_extraparams>
<xml>





public String parsingXMLUsingJDOM1(String strXml) throws Exception {
        String xmlResponse = null;
        SAXBuilder builder = new SAXBuilder();
        InputStream is = new ByteArrayInputStream(strXml.getBytes());


        try {


            Document document = (Document) builder.build(is);
            Element rootNode = document.getRootElement();
            List list = rootNode.getChildren("x_extraparams");
            for (int i = 0; i < list.size(); i++) {
            Element node = (Element) list.get(i);
            xmlResponse = node.getValue();


            String array[]=xmlResponse.split(",");


            xmlResponse=array[0].split("=").length > 1 ? array[0].split("=")[1]:"";
            xmlResponse+=",";
            xmlResponse+=array[1].split("=").length> 1 ? array[1].split("=")[1]:"";




            }
        } catch (IOException io) {
            System.out.println(io.getMessage());
        }  finally {


            if (builder != null) {
                builder = null;
            }
            if (is != null) {
                try {




                    is.close();
                    is = null;
                } catch (Exception e) {
                    e.printStackTrace();
                    ;
                }
            }
        }


        return xmlResponse;
    }


       Main Class :

      Simply Pass XML String to that specific method.

        SYN :     

 public class Dasari
{
 public static void main(String []a)
                      {

Dasari d=new Dasari();

d.parsingXMLUsingDOM("Pass XML String here ");

............ print the output here -----------
                        
             
                       }


}

  

Cheers Dasari




Happy Learning !!!



No comments:

Post a Comment