xml Converting to java using JDOM

Ads

 
 

Share on Google+Share on Google+

hamzah
xml Converting to java using JDOM
1 Answer(s)      5 years and 8 months ago
Posted in : Java Beginners

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

Ads
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
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
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 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
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
JDOM, JDOM Tutorial and example code.
will learn XML processing with JDOM API. The java examples discussed here explains... for manipulating XML files within Java Program. The JDOM API supports XPath and XSLT... An Overview of the XML-APIs Reading an XML document using JDOM Parsing
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
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
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
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: http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net/xml/getting
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> <
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
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.
Interceptors Configuration using Java or XML
In this section, you will learn about how to configure interceptors using Java or XML
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
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
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
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
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
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
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
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