No problem, it allowed me to dig more on this topic, on an angle I haven't tried before, that allowed me to find the dom.ls package which I didn't know before.
The good news is that the new output is now correctly indented!
- import java.io.*;
- import javax.xml.parsers.*;
- import org.w3c.dom.*;
- import org.w3c.dom.ls.DOMImplementationLS;
- import org.w3c.dom.ls.LSOutput;
- import org.w3c.dom.ls.LSSerializer;
- static final String NAMESPACE = "http://www.daisy.org/z3986/2005/ncx/";
- static final String NAMESPACE2 = "http://www.example.com/";
- static final String ROOT_ELEMENT = "xml"; // Level 0 tag name
- public static void createXMLFile(String fileName, boolean bUseNamespace)
- {
- // Set up the document builder
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = null;
- try
- {
- docBuilder = factory.newDocumentBuilder();
- }
- catch (ParserConfigurationException e)
- {
- System.out.println(e);
- }
- // Create document
- Document document = docBuilder.newDocument();
- DOMImplementation di = document.getImplementation();
- Element root = document.createElementNS(NAMESPACE, ROOT_ELEMENT);
- if (bUseNamespace)
- {
- DocumentType doctype = di.createDocumentType(
- ROOT_ELEMENT, // Qualified name // name
- "-//NISO//DTD xml 2005-1//EN", // publicId
- "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd" // systemId
- );
- document.appendChild(doctype);
- }
- document.appendChild(root);
- Attr rootAttr1 = document.createAttribute("version");
- rootAttr1.setValue("2005-1");
- root.setAttributeNode(rootAttr1);
- Attr rootAttr2 = document.createAttribute("xml:lang");
- rootAttr2.setValue("en-US");
- root.setAttributeNodeNS(rootAttr2);
- Element child1 = document.createElement("one-child");
- child1.setTextContent("Content of one child");
- root.appendChild(child1);
- // Don't re-use Element!
- Element child2 = document.createElement("one-child");
- child2.setTextContent("Another content\nof one child");
- Attr childAttr = document.createAttribute("a");
- childAttr.setValue("Fooo");
- child2.setAttributeNode(childAttr);
- root.appendChild(child2);
- Element child3 = document.createElement("other-child");
- child3.setAttribute("param", "Empty tag");
- root.appendChild(child3);
- Element child4 = document.createElement("other-child");
- child4.setTextContent("Content of the other child");
- child4.setAttribute("param", "Some value");
- root.appendChild(child4);
- Attr otherAttribute = null;
- if (bUseNamespace)
- {
- otherAttribute = document.createAttributeNS(NAMESPACE2, "attrNS");
- }
- else
- {
- otherAttribute = document.createAttribute("attrNoNS");
- }
- otherAttribute.setValue("Yo Man!");
- child4.setAttributeNode(otherAttribute);
- DOMImplementationLS implLS = (DOMImplementationLS) di.getFeature("LS", "3.0");
- LSSerializer serializer = implLS.createLSSerializer();
- serializer.getDomConfig().setParameter("format-pretty-print", true);
- LSOutput lsOutput = implLS.createLSOutput();
- lsOutput.setEncoding("UTF-8");
- // Output the result
- try
- {
- PrintWriter pw = new PrintWriter(fileName, "UTF-8");
- lsOutput.setCharacterStream(pw);
- serializer.write(document, lsOutput);
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Error on file: " + e);
- }
- catch (UnsupportedEncodingException e)
- {
- System.out.println("Error: " + e);
- }
- }