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 JDOMxml 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 XMLJava 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 XMLJava 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 BeginnersConverting 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
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 stringConverting 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 XMLConverting 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 javaaccessing
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 - XMLXML 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
read xml using javaread
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>
<
xml file reading using javaxml 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.
converting string to double in javaconverting 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 javahow 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 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 ProgramSending 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 ProgramSending 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">
<?
xml version="1.0" encoding="UTF-8" ?>
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVAHOW 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 xpathXML 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
read XML file and display it using java servletsread
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
converting image to byte[] - Java Beginnersconverting 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
Processing XML with JavaProcessing
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 DOMJava 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 SAXJava 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
parsing xml using saxparsing
xml using sax how to get values if the root elements are same and their attributes are different