Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
XML and Related Technologies
xml to java objects
Hamzah khammash
Greenhorn
Posts: 23
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello , <?xml version="1.0" encoding="UTF-8"?> <School name="Aloroba" > <class id ="1"> <Name>math</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> <class id="2"> <Name>phisics</Name> <Student id="1"> <Name>Hamzah</Name> <Address> <id>14421</id> </Address> </Student> <Student id="2"> <Name>khammash</Name> <Address> <id>144213</id> </Address> </Student> <Student id="3"> <Name>Hani</Name> <Address> <id>144151</id> </Address> </Student> <Student id="4"> <Name>kim</Name> <Address> <id>1442131</id> </Address> </Student> <Student id="5"> <Name>Viper</Name> <Address> <id>144121312</id> </Address> </Student> </class> <class id="3"> <Name>Java</Name> <Student id="1"> <Name>Hamzah</Name> <Address> <id>144213213</id> </Address> </Student> <Student id="2"> <Name>khammash</Name> <Address> <id>144871</id> </Address> </Student> <Student id="3"> <Name>Hani</Name> <Address> <id>1441879</id> </Address> </Student> <Student id="4"> <Name>kim</Name> <Address> <id>144189798</id> </Address> </Student> <Student id="5"> <Name>Viper</Name> <Address> <id>1441454</id> </Address> </Student> </class> </School>
Java
code :
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; // TODO: Auto-generated Javadoc /** * The Class PlayWithJDom. */ public class PlayWithJDom { /** The cls. */ static Classe cls = new Classe(); /** The school. */ static School school = new School(); /** The xml file. */ static File xmlFile; /** The builder. */ static SAXBuilder builder; /** * The main method. * * @param args * the arguments * @throws JDOMException * the jDOM exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void main(String[] args) throws JDOMException, IOException { builder = new SAXBuilder(); xmlFile = new File( "C:\\Users\\h.khammash\\Desktop\\Studening workspace\\XMLParsing\\Schools.xml"); readXml(); System.out.println(school.getName()); } /** * Read xml. */ public static void readXml() { try { Document document = (Document) builder.build(xmlFile); Element rootNode = document.getRootElement(); String schoolName = rootNode.getAttribute("name").getValue(); school.setName(schoolName); 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()); } } }
i have xml file above and 4 classes school ,classe ,student,address and the above class i need a way to put xml constants to java objects for these class ?
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
missing one record from excel to access using datasources for both drivers
JComboBox display issue when changing models
Find matches in two collections
JDOM and Xml
Linked List equals method
More...