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









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
Reading an XML document using JDOM
Reading an XML document using JDOM   ..., it is a tree based Java api. JDOM represents an XML document as a tree composed... by using JDOM.eplace a node with existing node in a DOM document. JDOM is used
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
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
Converting HTML to XML - Java Beginners
Converting HTML to XML  Hi, I am generating an HTML file (JSP from a Java Struts App.) I need to figure out a way to create an XML file from that HTML document. If you can refer me to some Java Code, that would
how to parse xml document which contains elements with namespace using JDOM - XML
is inside so how can i parse this xml using JDOM,java. Thanks Shree  Hi...how to parse xml document which contains elements with namespace using... Lucknow "parsingxml.jsp" Parsing of xml using DOM Parser
How Compare 2 xml files with JDOM - Java Beginners
How Compare 2 xml files with JDOM  Hi I'm want help on how can I compare 2 xml files using JDOM in java and identify whether have the same content or not My files can simply be File1 File2
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
JDOM, JDOM Tutorial and example code.
will learn XML processing with JDOM API. The java examples discussed here explains... Java API for manipulating XML files within Java Program. The JDOM API supports... and output XML data. The JDOM is specially designed for the Java platform
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
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
accessing xml using java
accessing xml using java  I need to retrieve some elements in xml file using java   Hi Friend, Please visit the following links:ADS_TO_REPLACE_1 http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net
XML parsing using Java - XML
XML parsing using Java  I'm trying to parse a big XML file in JAVA..." in element "MIRate"(THE XML code is below).once he enters coverage rate I need... for it skipping all the elements in Table 1. I'm usin using saxparser, startelement
converting an xml file in relational database objects
converting an xml file in relational database objects  converting an xml file in relational database objects
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> <
Maven dependency for jdom - jdom version b8 is released. Learn to use jdom version b8 in Maven based Java projects
is b8. Developer can use this version ( jdom - jdom version b8 ) in their Java... b8 in project? Steps to use  jdom - jdom version b8 in Java projects. Follow the step by step tutorial for using the latest version of jdom - jdom
Maven dependency for jdom - jdom version b9 is released. Learn to use jdom version b9 in Maven based Java projects
is b9. Developer can use this version ( jdom - jdom version b9 ) in their Java... b9 in project? Steps to use  jdom - jdom version b9 in Java projects. Follow the step by step tutorial for using the latest version of jdom - jdom
Maven dependency for jdom - jdom version 1.0 is released. Learn to use jdom version 1.0 in Maven based Java projects
is 1.0. Developer can use this version ( jdom - jdom version 1.0 ) in their Java... version 1.0 in project? Steps to use  jdom - jdom version 1.0 in Java projects. Follow the step by step tutorial for using the latest version of jdom - jdom
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.
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
parsing xml file using java code
parsing xml file using java code  parsing a xml file 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
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
Interceptors Configuration using Java or XML
In this section, you will learn about how to configure interceptors using Java or XML
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
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
Java XML
using JDOM | Parsing The XML File Using JDOM Parser in JSP...Java XML In this section we will learn how to manipulate XML files in Java program. Java provides excellent API for manipulating the XML files. In your 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 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
JDOM Introduction
for manipulating XML documents from within the Java programs. The JDOM api... for processing xml documents. The JDOM API is designed specifically for Java platform... Reading an XML document using JDOM Parsing The XML
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
JDOM Element Example, How to set attribute of xml element in java.
JDOM Element Example, How to set attribute of xml element in java. In this tutorial, we will see how to set attribute of xml element in java with the help of JDOM library. JDOM is used for parsing, creating, manipulating
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
JDOM Element Example, How to remove attribute of xml element in java.
JDOM Element Example, How to remove attribute of xml element in java. In this tutorial, we will see how to remove attribute of xml element in java with the help of JDOM library. It is a java tool kit for xml parsing. With the help
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
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 HTML to image - Java Beginners
Converting HTML to image   Is it possible to convert html to image with java code ?   Hi friend, Please give the problem in details...://www.roseindia.net/java/ Thanks
converting image to byte[] - Java Beginners
converting image to byte[]  Dear Sir Can you please tell me a solution for converting a java.awt.Image to byte[] eg: java.awt.Image... for converting } Please give me a solution Thanks in Advance. Rahul
losing precision converting from java BigDecimal to double
losing precision converting from java BigDecimal to double  losing precision converting from java BigDecimal to double
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.xmlADS_TO_REPLACE_1 <?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.xmlADS_TO_REPLACE_1 <?xml version="1.0" encoding="UTF-8
Version of jdom>jdom dependency
List of Version of jdom>jdom dependency
Version of jdom>jdom- dependency
List of Version of jdom>jdom- dependency
JDOM example in java, How to read a xml file in java.
JDOM example in java, How to read a xml file in java. In this tutorial, we will see how to  read a xml file in java. Code. Student.xml <?xml version="1.0"?> <Student >
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different

Ads