import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.FlyweightProcessingInstruction;
public class XmlOutput {
public static void writeDocument(Document doc, File df) {
df.getParentFile().mkdirs();
try {
//Step 1
OutputFormat format = OutputFormat.createPrettyPrint();
//Step 2
format.setTrimText(false);
//Step 3
format.setIndentSize(4);
XMLWriter writer = new XMLWriter(new OutputStreamWriter(
new FileOutputStream(df), “UTF-8”), format);
FlyweightProcessingInstruction xmlDeclaration = new FlyweightProcessingInstruction(
“xml”,
“version=\”1.0\” encoding=\”UTF-8\” standalone=\”no\” “);
writer.write(xmlDeclaration);
writer.write(doc.processingInstructions());
writer.write(doc.getDocType());
writer.write(doc.getRootElement());
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
June 21, 2009 at 12:17 am
Is that the code for building the XML for a site?
June 21, 2009 at 11:00 am
Thanks for your comment.
When you will go to write an Xml file from Java with the dom4j you will need to do
3 things stated above (to ensure proper indentation) before writing to the file system.
I have given here a static method that will write your XML document in the file system with proper indentation.
Now come to your question. Yes, you can use this method to build Xml file for any site.
But you will need another conversion methodology for example XSLT to build Html file from the Xml file.
Thanks.