How to save Document object into .xml file

1) Iam converting JDBC ResultSet into XML.

2) Iam done with converting JDBC ResultSet to Document.

3) My query is, how can I save it into a .xml file using FileOutPutStream. For your perusal I've attached the code.

4) I've converted it into string and stored in xml file. But I don't find any wellformed xml content in the xml file

public Document getCustomerList()
{
      Document document = null;
      try
      {
            statement = connection.createStatement();
            resultset = statement.executeQuery("select *from temp");
            document = toDocument(resultset);
            resultset.close();
            statement.close();
      }
      catch(Exception e)
      {
            e.printStackTrace();
      }
      return document;
}

public Document toDocument(ResultSet resultset) throws ParserConfigurationException, SQLException
{
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.newDocument();
      ResultSetMetaData rsmd = resultset.getMetaData();
      int colCount = rsmd.getColumnCount();

      Element results = document.createElement("Results");
      document.appendChild(results);
      while(resultset.next())
      {
            Element rows = document.createElement("Rows");
            results.appendChild(rows);

            for(int i = 1;i <= colCount;i++)
            {
                  String columnName = rsmd.getColumnName(i);
                  Object value = resultset.getObject(i);
                  Element node = document.createElementcolumnName);
                  node.appendChild(document.createTextNode(value.toString()));
                  rows.appendChild(node);
            }
      }
      return document;
}

public static void main(String[] args) throws Exception
{
      JavaXML jxml = new JavaXML();
      Document document = jxml.getCustomerList();

      byte buffer[] = (document.toString()).getBytes();
      OutputStream xmlfile = new FileOutputStream("xmlfile.xml");
      xmlfile.write(buffer);
      xmlfile.close();

}
ramnramAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
Source source = new DOMSource(document);
Result result = new StreamResult(new File("xmlfile.xml"));
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NaeemgCommented:
Try following code to save ur DOM document structure to .xml document.

     String fileNamePath = "myDoc.xml";
     StringBuffer thisData = new StringBuffer(document.toString());
     try
     {
       FileOutputStream outputFile = new FileOutputStream(fileNamePath); // stores an output stream reference
       FileChannel outChannel = outputFile.getChannel(); // Channel for file stream
       try
       {
         //** finally write the buffered bytes to the file using file channel
          outChannel.write(ByteBuffer.wrap(thisData.toString().getBytes("UTF-8")));
       }

       catch(IOException ex1)
       {
         //ex1.getMessage();
       }
     }
     catch(FileNotFoundException ex)
     {
       //ex.getMessage();
     }


//Naeem Shehzad Ghuman
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.