We help IT Professionals succeed at work.
Get Started

XML declaration doesn't get added to my generated XML file?!

snajalm asked
Last Modified: 2012-05-12
The following code does not add the XML declaration to my generated XML file even though I declared output properties to take in those parameters.  How can I make sure the "XML declaration" gets passed in??
public static void generateXMLFile(ResultSet rs, String xmFileName) throws SQLException, ParserConfigurationException, TransformerConfigurationException, TransformerException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element results = doc.createElement("NewDataSet");
        String rowName = xmFileName.substring(0, xmFileName.indexOf("."));
        int temp = 100;
        File folder = new File("C:\\windows\\temp\\lmb");
        try {
            if (folder.mkdir()) {
                System.out.println("Directory Created...");
            } else {
                System.out.println("Directory is not created!");
        } catch (Exception e) {

        File resultXML = new File("C:\\WINDOWS\\Temp\\ida\\" + xmFileName);

        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs.next()) {
            Element row = doc.createElement(rowName);
            for (int i = 1; i <= colCount; i++) {
                String columnName = rsmd.getColumnName(i);
                Object value = rs.getObject(i);
                if (value != null) {
                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString() + "\n"));
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        sr = new StreamResult(resultXML);
        transformer.transform(domSource, sr);


Open in new window

Watch Question
Awarded 2011
Awarded 2011
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE