|
Writing a JDOM tree with XMLOutputter |
|
|
There are several ways and formats for the JDOM document to be save in.
In order to write an XML file from a documetn we have to use class XMLOutputter.
It allows to save the document either to a file or to a String object in memory.
Besides entire document we can save separate JDOM elements.
XML and String outputs are shown in the sample below:
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
/**
* This sample program showing how to create new
* JDOM document and save it to a file.
*/
public class Test {
public static void main(String[] args) {
try {
// first we have to create a document element:
Element order = new Element("purchase-order");
// ... the we can define a document:
Document document = new Document(order);
// we can event compose and insert our document type:
DocType type = new DocType("purchase-order");
document.setDocType(type);
// .. create some other contents ...
// save it to a file:
XMLOutputter out = new XMLOutputter();
java.io.FileWriter writer = new java.io.FileWriter("order.xml");
out.output(document, writer);
writer.flush();
writer.close();
// ... or save it to a string and print:
System.out.println(out.outputString(document));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.