
Hi All,
Good Morning, I have been working as a Manual Test engineer for last 4 years. Now i started learning java for automating some of the functionaries. And here goes my requirements.
Read huge complex xml and compare values with DB2 database and generate a report. So First of all i need to read xml using java . i did good research in google and came to know that there are many ways to read using different API like SAX, DOM , XOM Jar, XML Beans Jar and so on...
So i request you all to suggest best way to read XML ie SAX or Dom ? if possible sample code also.
WebTester.

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class ParseXmlFile{
public static void main(String argv[]){
ArrayList<Student> list=new ArrayList<Student>();
try{
File file = new File("c:\\file.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
document.getDocumentElement().normalize();
System.out.println("Root element "+ document.getDocumentElement().getNodeName());
NodeList node = document.getElementsByTagName("student");
System.out.println("Information of the students");
for(int i = 0; i < node.getLength(); i++){
Node firstNode = node.item(i);
if(firstNode.getNodeType()== Node.ELEMENT_NODE){
Element element = (Element) firstNode;
NodeList firstNameElemntList = element.getElementsByTagName("name");
Element firstNameElement = (Element) firstNameElemntList.item(0);
NodeList firstName = firstNameElement.getChildNodes();
Node n1=firstName.item(0);
String name=n1.getNodeValue();
NodeList lastNameElementList = element.getElementsByTagName("address");
Element lastNameElement = (Element)lastNameElementList.item(0);
NodeList lastName = lastNameElement.getChildNodes();
Node n2=lastName.item(0);
String address=n2.getNodeValue();
list.add(new Student(name,address));
}
}
for(Student st:list){
System.out.println(st.getName()+"\t"+st.getAddress());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Parse XML using JDOM
import java.io.*;
import org.jdom.*;
import java.util.*;
import org.jdom.input.SAXBuilder;
public class ReadXMLUsingJDOM {
public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:\\student.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List list = rootNode.getChildren("class");
for (int i = 0; i < list.size(); i++) {
Element node = (Element) list.get(i);
String classid = node.getAttribute("id").getValue();
String name = node.getChildText("Name");
System.out.println("Class ID: "+classid);
System.out.println("Name: "+name);
List list1 = node.getChildren("Student");
for (int j = 0; j < list1.size(); j++) {
Element node1 = (Element) list1.get(j);
String sid = node1.getAttribute("id").getValue();
String sname = node1.getChildText("Name");
System.out.println("Student ID: "+sid);
System.out.println("Student Name: "+sname);
List list2 = node1.getChildren("Address");
for (int k = 0; k < list2.size(); k++) {
Element node2 = (Element) list2.get(k);
String saddress = node2.getChildText("id");
System.out.println("Address: "+saddress);
}
}
}
}
catch(Exception e){
System.out.println("Execption " + e.getMessage());
}
}
}


Thanks a lot for the quick reply.
From above two codes i think reading xml using JDOM is simple. But my XML size ranges from 30 MB to 60 MB. Will JDOM handle such huge load ?
Thanks in advance

Finally i decided to use JDOM .
i am using following xml
<Root>
<Maste-item-detail coder="afrffica">
<Maste-item-detail coder="afrffica">
And my java code is here SAXBuilder builder = new SAXBuilder(); File xmlFile = new File("H:\sample1.xml");
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
System.out.println ("Root element of the doc is " + rootNode.getName());
List MasterItemlist = rootNode.getChildren("Maste-item-detail");
System.out.println("Total Single Items : " + MasterItemlist.size());
for (int i = 0; i < MasterItemlist.size(); i++) {
Element node = (Element) MasterItemlist.get(i);
Element innernode = (Element) node.getChildren("content");
List name = innernode.getChildren("name");
System.out.println("First Name : " + innernode.getChildText("name"));
}
and i am getting following error Exception in thread "main" java.lang.ClassCastException: org.jdom.ContentList$FilterList cannot be cast to org.jdom.Element at ReadXMLFile.main(ReadXMLFile.java:28) Root element of the doc is Root Total Single Items : 3
please help me to get it right
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.