hello friends i am posting here awesome example of Jdom Parser using which we can Marshall/unmarshall java objects into/from xml .
We can use Differant API's for converting java object tree into xml or vice versa
Jdom parser is java based Document object Model Parser using which we can parse xml data .i am demostrating simple example which will be used to marshel and unmarshel java objects:
The sample xml file which i am using is as follows:
<?xml version="1.0"?>
<company>
<staff>
<firstname>nisarg</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
the class will placed in org.jdom package :
package org.jdom;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.*;
import java.util.*;
//jdom parser implementation
public class ReadXml
{
public static void main(String args[])
{
SAXBuilder builder =new SAXBuilder();
File xmlFile=new File("D:\\company.xml");
try
{
Document document =(Document)builder.build(xmlFile);
Element rootNode =document.getRootElement();
List list=rootNode.getChildren();
for(int i=0;i<list.size();i++)
{
Element node =(Element) list.get(i);
System.out.println("First Name : " + node.getChildText("firstname"));
System.out.println("Last Name : " + node.getChildText("lastname"));
System.out.println("Nick Name : " + node.getChildText("nickname"));
System.out.println("Salary : " + node.getChildText("salary"));
}
}
catch(IOException ie)
{
System.out.println(ie.getMessage());
}
catch(JDOMException e)
{
System.out.println(e.getMessage());
}
}
}
package org.demo;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
//jdom parser(Dom) will load entire document into memory
public class XmlFileCreator
{
public static void main(String args[]) throws IOException
{
final String XmlFilePath="D:\\ocjp\\EmployeeData.xml";
try
{
Element company =new Element("company");
Document document =new Document(company);
Element employee =new Element("employee");
employee.addContent(new Element("firstname").setText("kesha"));
employee.addContent(new Element("lastname").setText("pathak"));
employee.addContent(new Element("department").setText("Computer"));
employee.addContent(new Element("age").setText("23"));
document.getRootElement().addContent(employee);
Element employee1 = new Element("employee");
employee1.setAttribute(new Attribute("id", "2"));
employee1.addContent(new Element("firstname").setText("John"));
employee1.addContent(new Element("lastname").setText("Filis"));
employee1.addContent(new Element("department").setText("Human Resources"));
employee1.addContent(new Element("age").setText("28"));
document.getRootElement().addContent(employee1);
XMLOutputter xml=new XMLOutputter();
xml.setFormat(Format.getPrettyFormat());
xml.output(document,new FileWriter(XmlFilePath));
System.out.println("XML File was created successfully!");
}
catch(Exception e)
{
}
}
}
We can use Differant API's for converting java object tree into xml or vice versa
Jdom parser is java based Document object Model Parser using which we can parse xml data .i am demostrating simple example which will be used to marshel and unmarshel java objects:
The sample xml file which i am using is as follows:
<?xml version="1.0"?>
<company>
<staff>
<firstname>nisarg</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
the class will placed in org.jdom package :
package org.jdom;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.*;
import java.util.*;
//jdom parser implementation
public class ReadXml
{
public static void main(String args[])
{
SAXBuilder builder =new SAXBuilder();
File xmlFile=new File("D:\\company.xml");
try
{
Document document =(Document)builder.build(xmlFile);
Element rootNode =document.getRootElement();
List list=rootNode.getChildren();
for(int i=0;i<list.size();i++)
{
Element node =(Element) list.get(i);
System.out.println("First Name : " + node.getChildText("firstname"));
System.out.println("Last Name : " + node.getChildText("lastname"));
System.out.println("Nick Name : " + node.getChildText("nickname"));
System.out.println("Salary : " + node.getChildText("salary"));
}
}
catch(IOException ie)
{
System.out.println(ie.getMessage());
}
catch(JDOMException e)
{
System.out.println(e.getMessage());
}
}
}
=> example of converting java object tree into xml file :
package org.demo;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
//jdom parser(Dom) will load entire document into memory
public class XmlFileCreator
{
public static void main(String args[]) throws IOException
{
final String XmlFilePath="D:\\ocjp\\EmployeeData.xml";
try
{
Element company =new Element("company");
Document document =new Document(company);
Element employee =new Element("employee");
employee.addContent(new Element("firstname").setText("kesha"));
employee.addContent(new Element("lastname").setText("pathak"));
employee.addContent(new Element("department").setText("Computer"));
employee.addContent(new Element("age").setText("23"));
document.getRootElement().addContent(employee);
Element employee1 = new Element("employee");
employee1.setAttribute(new Attribute("id", "2"));
employee1.addContent(new Element("firstname").setText("John"));
employee1.addContent(new Element("lastname").setText("Filis"));
employee1.addContent(new Element("department").setText("Human Resources"));
employee1.addContent(new Element("age").setText("28"));
document.getRootElement().addContent(employee1);
XMLOutputter xml=new XMLOutputter();
xml.setFormat(Format.getPrettyFormat());
xml.output(document,new FileWriter(XmlFilePath));
System.out.println("XML File was created successfully!");
}
catch(Exception e)
{
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<company>
<employee>
<firstname>kesha</firstname>
<lastname>pathak</lastname>
<department>Computer</department>
<age>23</age>
</employee>
<employee id="2">
<firstname>John</firstname>
<lastname>Filis</lastname>
<department>Human Resources</department>
<age>28</age>
</employee>
</company>
this is how JDOM works
The basic differance between Jdom & other parser is it loads entire document into memory.so that if size of xml is small than in that case it is more preferable.
ThAnks
No comments:
Post a Comment