0
votes

For sorry, I don't know how to attempt to do such thing.

public void writeXML(String tableName) {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement(tableName);
        doc.appendChild(rootElement);

        for(Map m: rows){
            Element parent = doc.createElement((String) m.get("ownerNode"));
            rootElement.appendChild(parent);
            for(String s: colNames){
                String key = (String) m.get(s);
                System.out.println(key);
                Element innerNode = doc.createElement(s);
                innerNode.appendChild(doc.createTextNode((String) m.get(s)));
                parent.appendChild(innerNode);
            }
        }



        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(tableName + ".xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

    } catch (ParserConfigurationException pce) {
    } catch (TransformerException tfe) {
    }

}

this is my code to write the body of the xml so it would be greatly appreciated if can anybody help me to add XML version,encoding, doctype and DTD reference with DOM parser

1

1 Answers

8
votes

Code showing the default behavior and the way to change that:

String inputXml = "<root><value>Test</value></root>";

// Build DOM tree for input XML
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document document = domBuilder.parse(new InputSource(new StringReader(inputXml)));

// Print DOM XML using default settings
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(System.out));

System.out.println();   System.out.println();

// Print DOM XML using specific settings
document.setXmlVersion("1.1");
transformer.setOutputProperty(OutputKeys.ENCODING, "windows-1252");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "ROOT-VALUE");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "RootValue.dtd");
transformer.transform(new DOMSource(document), new StreamResult(System.out));

Output (using Java 1.8.0_51)

<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><value>Test</value></root>

<?xml version="1.1" encoding="windows-1252" standalone="no"?><!DOCTYPE root PUBLIC "ROOT-VALUE" "RootValue.dtd">
<root><value>Test</value></root>