How to save Document object into .xml file

Posted on 2004-10-24
Last Modified: 2011-10-03
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;
            statement = connection.createStatement();
            resultset = statement.executeQuery("select *from temp");
            document = toDocument(resultset);
      catch(Exception e)
      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");
            Element rows = document.createElement("Rows");

            for(int i = 1;i <= colCount;i++)
                  String columnName = rsmd.getColumnName(i);
                  Object value = resultset.getObject(i);
                  Element node = document.createElementcolumnName);
      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");

Question by:ramnram
    LVL 92

    Accepted Solution

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

    Expert Comment

    Try following code to save ur DOM document structure to .xml document.

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

           catch(IOException ex1)
         catch(FileNotFoundException ex)

    //Naeem Shehzad Ghuman

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    fizzArray  challenge 1 22
    only14 challenge 19 52
    EvenOdd challenge 10 50
    changeXy challenge 13 26
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now