Search This Blog

Loading...

Wednesday, January 20, 2010

How to create XML file using JDOM in java

This example creates xml file using JDOM apis .

import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class CreateXML {
public static void main(String a[]){
OutputStream fos = null;
Document doc = new Document();
Element root = new Element("emps");
doc.setRootElement(root);
// create emp element
Element emp = new Element("emp");
emp.setAttribute("id", "1016");
Element name = new Element("name");
name.setText("Nataraj");
emp.addContent(name);
Element dep = new Element("dep");
dep.setText("development");
emp.addContent(dep);
//Add emp element to root element
root.addContent(emp);
Element emp1 = new Element("emp");
emp1.setAttribute("id", "1015");
Element name1 = new Element("name");
name1.setText("Nagesh");
emp1.addContent(name1);
Element dep1 = new Element("dep");
dep1.setText("accounts");
emp1.addContent(dep1);
//Add emp element to root element
root.addContent(emp1);
try{
fos = new FileOutputStream("c://test.xml");
new XMLOutputter().output(doc, fos);
} catch(Exception ex){
ex.printStackTrace();
} finally {
try{
if(fos != null) fos.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
}
}

OUTPUT:

<emps><emp id="1016"><name>Nataraj</name><dep>development</dep></emp><emp id="1015"><name>Nagesh</name><dep>accounts</dep></emp></emps>

Tuesday, January 19, 2010

How to read XML file using JDOM in java

This article gives you an example to read xml file using JDOM api. We will read xml file and print the values on to the console:

Save below xml into emp.xml file.

<emps>
<emp id="1016">
<name>Nataraj</name>
<dep>Accounts</dep>
</emp>
<emp id="1015">
<name>Nagesh</name>
<dep>Marketing</dep>
</emp>
</emps>


Java code to read xml file:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ParseXml {

public static void main(String a[]){

Document xmlDoc = null;
SAXBuilder builder = new SAXBuilder();
InputStream is = null;
Element root = null;
try {
is = new FileInputStream(new File("C:\\emp.xml"));
xmlDoc = builder.build(is);
if(xmlDoc != null){
root = xmlDoc.getRootElement();
System.out.println("Root element name: "+root.getName());
List
empList = root.getChildren("emp");
System.out.println("Emp Size: "+empList.size());
for(Element emp:empList){
System.out.println("Id: "+emp.getAttributeValue("id"));
Element name = emp.getChild("name");
System.out.println("Name: "+name.getText());
Element dep = emp.getChild("dep");
System.out.println("Deportment: "+dep.getText());
System.out.println("<--------------------------------------->");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}


OUTPUT:
-----------
Root element name: emps
Emp Size: 2
Id: 1016
Name: Nataraj
Deportment: Accounts
<--------------------------------------->
Id: 1015
Name: Nagesh
Deportment: Marketing
<--------------------------------------->

Monday, January 18, 2010

How to get ClassLoader or Resource in a java static method?

Follow the below steps to load property file in a java static method.

Properties prop = new Properties();

prop.load(YourClassName.class.getResourceAsStream("File path"));