Tuesday, April 2, 2013

Using JDOM API to create an XML document

2:52 AM 0
Using JDOM API to create an XML document

As we know JDOM is a very useful library to provide a complete , java based solution for accessing, manipulating , and outputting XML data from java code.

The following example simply describes how to create an XML file by using JDOM library.

package com.app;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class JDOMXml { public static void main(String[] args) { Element note = new Element("note");
Document document = new Document();
Element to = new Element("to"); to.setText("Unknown");
Element from = new Element("from");
Element heading = new Element("heading");
Element message = new Element("message");
message.setText("Urgent message");
XMLOutputter outputter = new XMLOutputter();
try {
outputter.output(document, new FileWriter("F:\\file.xml"));
System.out.println("Successfully create xml file.");
} catch (IOException e) {
System.out.println("Couldn't created xml file." + e.getMessage());

After running above programme it produces following outkput.

<?xml version="1.0" encoding="UTF-8"?>
  <message>Urgent message</message>

Read an XML document using JDOM API

2:50 AM 0
Read an XML document using JDOM API
to be continued...