Hello , I am new to java and JDom so i make a Xml file and i need help to read it from java using objects , my idea is i have school has a class,class has a 3 students ,student has a address and in java i have school,class,student,address class each one have a setter and getter , i dont know how to differentiate that this for example: line in xml should be inside a student object i will include the java code and xml code and i hope somebody helps me
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class PlayWithJDom {
static List cls = new ArrayList();
static List clas;
static School school = new School();
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder builder1 = new SAXBuilder();
Document doc = builder1
.build("C:\\Users\\h.khammash\\Desktop\\Studening workspace\\XMLParsing\\Schools.xml");
Element element = doc.getRootElement();
ReadClass(element);
}
public static void ReadClass(Element element) throws JDOMException,
IOException {
List list = element.getChildren();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
System.out.println(child.getValue().trim());
ReadClass(child);
}
}
}
and XML FILE :
<School>
<class id ="1">
<Name>Alsharef Husain</Name>
<Student id="1">
<Name>Hamzah</Name>
<Address>
<id>1441</id>
</Address>
</Student >
<Student id="2">
<Name>khammash</Name>
<Address>
<id>1442</id>
</Address>
</Student>
<Student id="3">
<Name>Hani</Name>
<Address>
<id>1443</id>
</Address>
</Student>
<Student id="4">
<Name>kim</Name>
<Address>
<id>1445</id>
</Address>
</Student>
<Student id="5">
<Name>Viper</Name>
<Address>
<id>14454</id>
</Address>
</Student>
</class>
</School>
Thanks
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:\\Schools.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());
}
}
}