Java Utililty Methods XML Document to File

List of utility methods to do XML Document to File

Description

The list of methods to do XML Document to File are organized into topic(s).

Method

voiddumpDocument(Document doc, String outName)
dump Document
DOMSource source = new DOMSource(doc);
File path = new File(System.getProperty("test.dir"), outName);
Result result = new StreamResult(new FileOutputStream(path));
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.transform(source, result);
booleanstoreXML(Document doc, File file)
store XML
try {
    file.getParentFile().mkdirs();
    String s = file.getAbsolutePath();
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc),
            new StreamResult(new FileOutputStream(s)));
    return true;
} catch (Exception e) {
    return false;
...
voidstoreXml(final File xmlFile, final Document document)
store Xml
if (xmlFile == null) {
    throw new NullPointerException("xmlFile");
if (xmlFile.exists() && !xmlFile.canWrite()) {
    throw new IllegalArgumentException("xmlFile");
if (xmlFile.exists() && !xmlFile.isFile()) {
    throw new IllegalArgumentException("xmlFile");
...
voidwrite(Document doc, File out)
write
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    DocumentType dt = doc.getDoctype();
    if (dt != null) {
        String pub = dt.getPublicId();
        if (pub != null) {
            t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
        t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
    t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
    t.setOutputProperty(OutputKeys.INDENT, "yes"); 
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 
    Source source = new DOMSource(doc);
    Result result = new StreamResult(out);
    t.transform(source, result);
} catch (Exception e) {
    throw (IOException) new IOException(e.toString()).initCause(e);
} catch (TransformerFactoryConfigurationError e) {
    throw (IOException) new IOException(e.toString()).initCause(e);
voidwrite(final Document doc, final File file)
write
try {
    final DOMSource source = new DOMSource(doc);
    final StreamResult result = new StreamResult(file);
    final TransformerFactory factory = TransformerFactory.newInstance();
    final Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
    throw new RuntimeException(e);
...
voidwriteDataToFile(File file, Document doc)
write Data To File
try {
    Source source = new DOMSource(doc);
    Result result = new StreamResult(file);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4);
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(source, result);
...
StringwriteDoc(Document doc, File aFile)
if aFile is given, will write to file, otherwise return as String
String s = null;
try {
    OutputFormat format = new OutputFormat("XML", "UTF-8", true);
    if (aFile != null) {
        FileOutputStream out = new FileOutputStream(aFile);
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(doc);
        out.close();
...
voidwriteDoc(Document doc, File file)
write Doc
TransformerFactory transformerFactory = SAXTransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
voidwriteDocToFile(Document doc, String filename)
write Doc To File
Source source = new DOMSource(doc);
File file = new File(filename);
Result result = new StreamResult(file);
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute("indent-number", 2);
Transformer xformer = tFactory.newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
...
voidwriteDocToFile(Document doc, String fileName)
* Output Document to file ******************************************.
File encryptionFile = new File("c:\\temp\\" + fileName);
FileOutputStream f = new FileOutputStream(encryptionFile);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(f);
transformer.transform(source, result);
...