DOM Parsing

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.w3c.dom.NodeList;
public class domParsing{
public static void main(String args[]){
try{

File file=new File("C:/Anir.xml");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder dbr= dbf.newDocumentBuilder();
Document doc=dbr.parse(file);
doc.getDocumentElement().normalize();

System.out.println("Root element " + doc.getDocumentElement().getNodeName());
Node rootnode = doc.getDocumentElement();
System.out.println(rootnode.getNodeName());

// Steps for Getting id
NodeList db = doc.getElementsByTagName("DB");
Element dbElement = (Element)db.item(0);
NodeList childNodes = dbElement.getChildNodes();
System.out.println("DB : " + ((Node) childNodes.item(0)).getNodeValue());

// Steps for Getting ActionType
NodeList dbuser = doc.getElementsByTagName("DBUser");
Element dbuserElement = (Element)dbuser.item(0);
NodeList childNodes1 = dbuserElement.getChildNodes();
System.out.println("DBUser : " + ((Node) childNodes1.item(0)).getNodeValue());

// Steps for Getting ServiceType
NodeList dbpass = doc.getElementsByTagName("DBPass");
Element dbpassElement = (Element)dbpass.item(0);
NodeList childNodes2 = dbpassElement.getChildNodes();
System.out.println("DBPass : " + ((Node) childNodes2.item(0)).getNodeValue());

NodeList hbi = doc.getElementsByTagName("HeartBeatInterval");
Element hbiElement1 = (Element)hbi.item(0);
NodeList childNodes3 = hbiElement1.getChildNodes();
System.out.println("HeartBeatInterval : " + ((Node) childNodes3.item(0)).getNodeValue());

NodeList hbc = doc.getElementsByTagName("HeartBeatCount");
Element hbcElement1 = (Element)hbc.item(0);
NodeList childNodes4 = hbcElement1.getChildNodes();
System.out.println("HeartBeatInterval : " + ((Node) childNodes4.item(0)).getNodeValue());

NodeList nodeLst=doc.getElementsByTagName("Primary");

for(int s=0;s Node fstNode = nodeLst.item(s);
if(fstNode.getNodeType()==Node.ELEMENT_NODE){
Element fstElmnt=(Element)fstNode;
NodeList fstNmElmntLst=fstElmnt.getElementsByTagName("Name");
Element fstNmElmnt= (Element)fstNmElmntLst.item(0);
NodeList fstNm= fstNmElmnt.getChildNodes();
System.out.println("Name : " + ((Node) fstNm.item(0)).getNodeValue());

NodeList lstNmElmntLst =fstElmnt.getElementsByTagName("IP");
Element lstNmElmnt=(Element)lstNmElmntLst.item(0);
NodeList lstNm= lstNmElmnt.getChildNodes();
System.out.println("IP : " + ((Node) lstNm.item(0)).getNodeValue());

NodeList lstNmElmntLst1 =fstElmnt.getElementsByTagName("DBIP");
Element lstNmElmnt1=(Element)lstNmElmntLst1.item(0);
NodeList lstNm1= lstNmElmnt1.getChildNodes();
System.out.println("DBIP : " + ((Node) lstNm1.item(0)).getNodeValue());

}

}
NodeList nodeLst2=doc.getElementsByTagName("Secondary");

for(int s1=0;s1 Node fstNode = nodeLst2.item(s1);
if(fstNode.getNodeType()==Node.ELEMENT_NODE){
Element fstElmnt1=(Element)fstNode;
NodeList fstNmElmntLst1=fstElmnt1.getElementsByTagName("Name");
Element fstNmElmnt1= (Element)fstNmElmntLst1.item(0);
NodeList fstNm1= fstNmElmnt1.getChildNodes();
System.out.println("Name : " + ((Node) fstNm1.item(0)).getNodeValue());

NodeList lstNmElmntLst1 =fstElmnt1.getElementsByTagName("IP");
Element lstNmElmnt1=(Element)lstNmElmntLst1.item(0);
NodeList lstNm1= lstNmElmnt1.getChildNodes();
System.out.println("IP : " + ((Node) lstNm1.item(0)).getNodeValue());

NodeList lstNmElmntLst2 =fstElmnt1.getElementsByTagName("DBIP");
Element lstNmElmnt2=(Element)lstNmElmntLst2.item(0);
NodeList lstNm2= lstNmElmnt2.getChildNodes();
System.out.println("DBIP : " + ((Node) lstNm2.item(0)).getNodeValue());

}

}
}catch (Exception e) {
e.printStackTrace();
}
}
}







Post a Comment

Previous Post Next Post