30 October 2012

dom parser

package check;

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



public class DOM_Demo {

Document dom;

public static void main(String[] args) throws Exception {

new DOM_Demo().go();

}


void ret(Node d){

NodeList n=d.getChildNodes();

{
System.out.println(d.getNodeName());
for@(int i=0;i{
    if(n.item(i).getNodeType()==1)  // 1 IS THE VALUE FOR TAG

ret(n.item(i));

}
else

{

if(n.item(i).getNodeValue().trim().length()!=0)
System.out.println("val : "+n.item(i).getNodeValue());
}

}


}

}

void go() throws Exception

{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder

DocumentBuilder db = dbf.newDocumentBuilder();
//parse using builder to get DOM representation of the XML file

dom = db.parse("C:/Users/shasharm/Desktop/ibf/NS_ABIS_IRP_IP_E1.xml");

Element d = dom.getDocumentElement();

ret(d);

}

}





Delicious add to del.icio.us saved by 0 users

No comments:

Post a Comment