Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Write a DOM Document as Formatted XMLs file
XMLWriter, SAXReader,
OutputFormat, Example.
This example shows us how to write a DOM Document as Formatted XMLs file.
In this Example we formatted
the XML by using OutputFormat.
import java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import java.io.OutputStream;
import org.dom4j.Document;
import
org.dom4j.DocumentException;
import
org.dom4j.io.XMLWriter;
import
org.dom4j.io.OutputFormat;
import
org.dom4j.io.SAXReader;
public class
XmlWriterReader {
public
static String CR =
System.getProperty("line.separator");
private XmlUtility() {
}
public static void
writeDOM(Document doc, File file)
throws IOException
{
OutputStream out = new FileOutputStream(file);
OutputFormat outformat = OutputFormat.createPrettyPrint();
outformat.setLineSeparator( CR );
outformat.setNewLineAfterNTags(4);
outformat.setNewlines(true);
XMLWriter writer = new XMLWriter(out, outformat);
writer.write(doc);
writer.flush();
}
public static Document
parseXml(File xmlFile){
SAXReader reader = new SAXReader();
Document document =null;
try {
document = reader.read (xmlFile);
} catch (DocumentException e ){
}
return document;
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.