xml Converting to java using JDOM

Ads
 

xml Converting to java using JDOM

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

View Answers

January 11, 2012 at 3:46 PM

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());
    }
  }
}

Ads









Related Tutorials/Questions & Answers:
xml Converting to java using JDOM
xml Converting to java using JDOM  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... should be inside a student object i will include the java code and xml code and i
Java and converting a PDF to XML
Java and converting a PDF to XML  I reviewed your example - https://www.roseindia.net/tutorial/java/xml/pdftoXML.html - and on many users group, it receives high praise. I understand it uses iText but I am having a problem
Advertisements
Java and converting a PDF to XML
Java and converting a PDF to XML  I reviewed your example - https://www.roseindia.net/tutorial/java/xml/pdftoXML.html - and on many users group, it receives high praise. I understand it uses iText but I am having a problem
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know...(); } } }   Parse XML using JDOM import java.io.*; import org.jdom..../xmlParsing/java-xml-parsing-using-sax.shtml http://www.roseindia.net/xml/sax
Parsing The XML File Using JDOM Parser in JSP
Parsing The XML File Using JDOM Parser in JSP... with JDOM parser to parse the xml document. JDOM can read existing XML documents... native parser. XML parsing produces an xml parse tree from an XML document. Using
Converting XML to string
Converting XML to string  Hi Friends, I had an requirement in which a java program receives an xml messages from external web application such as (jsp, php) and then it converts the received xml message into a string. could
Converting PDF in to XML
Converting PDF in to XML  I have to convert PDF into XMl without any loss in text. Please suggest sth good
read xml using java
read xml using java  <p>to read multiple attributes and elements from xml in an order.... ex :component name="csl"\layerinterfacefile="poo.c... element to b printed... here is the xml code......................</p> <
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
Interceptors Configuration using Java or XML
In this section, you will learn about how to configure interceptors using Java or XML
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers.
how to create xml schema from xml doc using java
how to create xml schema from xml doc using java  i want to create xml schema from xml document programatically using java... am using Netbeans IDE 7.0 i hav created the xml document for a table of data from Database... now i
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
Sending and receiving xml message using Java Program
Sending and receiving xml message using Java Program  Hi Friends, I want to send and receive xml files between two java programs using wire format, could you suggest me the steps to be followed to acheive it or suggest some
Sending and receiving xml message using Java Program
Sending and receiving xml message using Java Program  Hi Friends, I want to send and receive xml files between two java programs using wire format, could you suggest me the steps to be followed to acheive it or suggest some
How to Split a large XML file using java?
How to Split a large XML file using java?  How can we split a 500MB Xml file?I know how to split xml file after reading the entire document in a file.Here we cannot load the entire file as it is a large file
converting an xml file in relational database objects
converting an xml file in relational database objects  converting an xml file in relational database objects
How to read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following xml using java.. In <Line>,data is in format of key... in advance <pre lang="xml"> &lt;?xml version="1.0" encoding="UTF-8" ?>
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA  H ello, i have an xml code, i need to save it into mysql 5.5 server database using java. i... as xml and retrieve it the same way i saved it. SOMEONE PLEASE HELP.... this is my
How to values from xml using java?
How to values from xml using java?  Hi All, I want to read value from following < Line> xml using java.. In < Line>,data is in format of key and value pair.. i want to read only values..could u plz help me
XML to DB2 Comparison using Java xpath
XML to DB2 Comparison using Java xpath  Hi , I am a manual tester...' products . i want to compare these xml values with db2 using Java. I have Good knowledge in C, C++ and Ok with java. I am planning to use xpath to get details
download xml file from website using java code
download xml file from website using java code  how to download xml file from website using java code
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
What XML JavaObject-XML transformation tools to use? JDOM, Dom4J, XOM, XStream, JAXB, JiBX, PojoXml
What XML JavaObject-XML transformation tools to use? JDOM, Dom4J, XOM, XStream... to transform java objects to XML and back from XML to Java Objects. Provided the JAVA Objects are predefined and the xml schema/DTD is predefined. What is the best
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong java servlet program protected void
Processing XML with Java
Processing XML using Java In this section we will see the API's available in Java for processing the XML. XML file is used on the internet to share... using JDOM | Parsing The XML File Using JDOM Parser in JSP
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file Students.xml <?xml version="1.0" encoding="UTF-8"?> <
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file Students.xml <?xml version="1.0" encoding="UTF-8"?> <
Reading xml file using dom parser in java with out using getelementby tag name
Reading xml file using dom parser in java with out using getelementby tag name  Hi, How to read the xml file using java with dom parser, but without using getelementbytag name, and also read the attribute values also. I had
Converting jsp variable to java variable
Converting jsp variable to java variable  Hi how to convert java script variable to java variable on same jsp page
losing precision converting from java BigDecimal to double
losing precision converting from java BigDecimal to double  losing precision converting from java BigDecimal to double

Ads