We help IT Professionals succeed at work.

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

snajalm
snajalm asked
on
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");
        doc.appendChild(results);
        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);
            results.appendChild(row);
            for (int i = 1; i <= colCount; i++) {
                String columnName = rsmd.getColumnName(i);
                Object value = rs.getObject(i);
                System.out.println(value);
                if (value != null) {
                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString() + "\n"));
                    row.appendChild(node);
                }
            }
        }
        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);

        System.out.println(sw.toString());
        temp++;
    }

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:

I guess this specifies to omit XML declaration:
 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Awarded 2011
Awarded 2011

Commented:
Awarded 2011
Awarded 2011

Commented:





This is the file I created with your code and changeing OMIT_XML_DECLARATION to "No"

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><NewDataSet><myXMLFile><One>a
</One><Two>b
</Two><Three>c
</Three><Four>d
</Four></myXMLFile><myXMLFile><One>a
</One><Two>b
</Two><Three>c
</Three><Four>d
</Four></myXMLFile><myXMLFile><One>a
</One><Two>b
</Two><Three>c
</Three><Four>d
</Four></myXMLFile></NewDataSet>

Open in new window


This is the code
(I didn't have any  ResultSet so I created the classes ForResultSet and ColumnNames
which emulated ResultSet and ResultMetaData):
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

public class GenerateXMLFile {

    public static void generateXMLFile(ForResultSet 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");
        doc.appendChild(results);
        String rowName = xmFileName.substring(0, xmFileName.indexOf("."));
        int temp = 100;

        File folder = new File("C:\\temp\\test\\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:\\temp\\test\\ida\\" + xmFileName);

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

        while (rs.next()) {
            Element row = doc.createElement(rowName);
            results.appendChild(row);
            for (int i = 1; i <= colCount; i++) {
                String columnName = rsmd.getColumnName(i);
                Object value = rs.getObject(i);
                System.out.println(value);
                if (value != null) {
                    Element node = doc.createElement(columnName);
                    node.appendChild(doc.createTextNode(value.toString() + "\n"));
                    row.appendChild(node);
                }
            }
        }
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        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);

        System.out.println(sw.toString());
        temp++;
    }

    public static void main(String[] args) {
       ForResultSet rs = new ForResultSet();
        try{
        generateXMLFile( rs, "myXMLFile.xml");
        }catch(Exception ex){
            ex.printStackTrace();
        }

    }

}

class ForResultSet {
    ColumnNames cn;
     ArrayList<ArrayList<String>> resSet = new ArrayList<ArrayList<String>>();
    int currentRec = -1;

public ForResultSet () {
        ArrayList<String> headers = new ArrayList<String>();
    headers.add("One");
    headers.add("Two");
    headers.add("Three");
     headers.add("Four");
    cn = new ColumnNames(headers);

    ArrayList<String> data1 = new ArrayList<String>();
    data1.add("a");
    data1.add("b");
    data1.add("c");
    data1.add("d");
     ArrayList<String> data2 = new ArrayList<String>();
       data2.add("a");
    data2.add("b");
    data2.add("c");
    data2.add("d");
      ArrayList<String> data3 = new ArrayList<String>();
       data3.add("a");
    data3.add("b");
    data3.add("c");
    data3.add("d");

    resSet.add(data1);
     resSet.add(data2);
     resSet.add(data3);







}
public boolean next() {
    currentRec++;
 if(currentRec == resSet.size())return false;
    else return true;

}

public Object getObject(int i) {
    return resSet.get(currentRec).get(i-1);
}

          public ColumnNames getMetaData() {
              return cn;
          }

}

class ColumnNames {
    ArrayList<String> cn;
    public ColumnNames(ArrayList<String> cn) {
        this.cn = cn;



    }

    public String getColumnName(int i){

        return cn.get(i-1);
    }


    public int getColumnCount() {
        return cn.size();
    }

}

Open in new window



 

Author

Commented:
Many thanks for your keen observation in resolving this issue!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.