zoe
zoe

Reputation: 945

using jdbc to export data into xml document

This may seem to be dumb to ask.. I'm very new to jdbc and trying to follow a tutorial. The tutorial is trying to store the XML data in a parsed form, provided by 'mapping.xml'. Below is the sample code:

 public static void main (String args[]) {
    Document mapping = null;
    Document dataDoc = null;
    Document newDoc = null;
      try {

          DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docbuilder = dbfactory.newDocumentBuilder();
          mapping = docbuilder.parse("mapping.xml");
          dataDoc = docbuilder.newDocument();

          //Instantiate the new Document
          newDoc = docbuilder.newDocument();
      } catch (Exception e) {
          System.out.println("Errors with creating document: "+e.getMessage());
       }   

However, after running through the codes, no new XML document is created, neither exception is caught. Can anyone help me out?

Thanks!!

Upvotes: 0

Views: 597

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340763

You aren't really storing any XML document here, you barely create a DOM representation of the XML document in memory. In order to actually store the file you need the following code snippet:

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Result result = new StreamResult(new File("output.xml"));
Source source = new DOMSource(dataDoc);
transformer.transform(source, result);

3rd line is crucial.

BTW ask someone to review your code:

  • Methods are way too long
  • Uou aren't handling exceptions correctly
  • DOM might not be the best choice for exporting large amounts of data

Upvotes: 1

Related Questions