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>
No comments:
Post a Comment