Java XML Transform Usage getEmptyXmlFile(File xmlFile)

Here you can find the source of getEmptyXmlFile(File xmlFile)

Description

creates a new xmlfile with a "reports" son for the reports usage

License

Open Source License

Parameter

Parameter Description
xmlFile the file to write to

Exception

Parameter Description
Exception an exception

Declaration

public static void getEmptyXmlFile(File xmlFile) throws Exception 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    /**/*from   w w w . j a  va  2 s.c o m*/
     * creates a new xmlfile with a "reports" son for the reports usage
     * 
     * @param xmlFile
     *            the file to write to
     * @throws Exception
     */
    public static void getEmptyXmlFile(File xmlFile) throws Exception {
        Element main;
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.newDocument();
        main = doc.createElement("reports");
        doc.appendChild(main);
        saveDocumentToFile(doc, xmlFile);
    }

    public static void saveDocumentToFile(Document doc, File file) throws Exception {
        if (file.getParent() != null) {
            mkdirs(file.getParent());
        }
        FileOutputStream fos = new FileOutputStream(file);
        Source source = new DOMSource(doc);
        Result result = new StreamResult(fos);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        xformer.transform(source, result);
        fos.close();
    }

    public static void mkdirs(String pathName) {
        File file = new File(pathName);
        file.mkdirs();
    }
}

Related

  1. dumpMetadataToSystemOut(IIOMetadata iiometa)
  2. encodeBase64(Element elm)
  3. getCalendarAsXsdDateTime(Calendar c)
  4. getContentText(Element element)
  5. getDate(final XMLGregorianCalendar xmlGregorianCalendar)
  6. getImplementationObject()
  7. getNormalizedReader(Reader reader)
  8. getSaxSource(File newFile, SAXParserFactory spf)
  9. getSchema(InputStream... inputs)