Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to save Document object into .xml file

Posted on 2004-10-24
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Accepted Solution

objects earned 375 total points
ID: 12397543
Source source = new DOMSource(document);
Result result = new StreamResult(new File("xmlfile.xml"));
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);

Expert Comment

ID: 12397567
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

610 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