Discussions

XML & Web services: output XML to a file - URGENT

  1. output XML to a file - URGENT (5 messages)

    Hello all,

    I am trying to create a XML document from my JAVA program. I have constructed the DOM tree in my program and need a way to put the contents in a file for review and reuse in another place. I want the output(.xml) in a file only and not in the database. Is there a way to do this ? Please let me know. Also let me know if there is any API or special methods to be handled.

    Thanks & appreciate your help.
    Bharani

    Threaded Messages (5)

  2. output XML to a file - URGENT[ Go to top ]

    What Java classes are you using for XML?

    I know how to do it with Xerces et al. It's very easy.

    Look for the serialization classes, they let you serialize to a stream, which can be a FileOutputStream. Serializing in this context means the textuate version of the XML.

    Calling .toString() on a Document object will NOT work (shame really.)

    Chz

    Tony
  3. output XML to a file - URGENT[ Go to top ]

    Thanks Tony,

    I am using the standard JAXP package and hence the org.w3c.com and javax.xml.parsers.*. BTW, I found a standard funciton in the IBM site that prints a DOM tree to stdout. It basically parses the DOM again and sensing the Node type prints accordingly. I just changed to get into a outputstream and temporarily solved the problem.
  4. output XML to a file - URGENT[ Go to top ]

    you can use tramsformation package to transform DOM tree to a output stream.
  5. output XML to a file - URGENT[ Go to top ]

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    // create a DOMSource from document object (xml)
    DOMSource source = new DOMSource(document);

    // write to a string
    java.io.StringWriter sw = new StringWriter();

    // use the transformer object
    StreamResult resultsw = new StreamResult(sw);
    transformer.transform(source, resultsw);
    resultsw.setWriter (sw);

    // create a new String variable (above)
    // convert the StringWriter object to String
    returnstr = sw.toString();
    // write to the standard output
    System.out.println(returnstr);

    // write to the standard output
    System.out.println(sw.toString());
  6. output XML to a file - URGENT[ Go to top ]

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(document);

    // write to a file
    File f = new File("local.xml");
    StreamResult resultfile = new StreamResult(f);
    transformer.transform(source, resultfile);


    ///////////////////////////////////////////////
    The above code writes the document object to the file local.xml.

    ///////////////////////////////////////////////
    My earlier posting in this thread shows how to convert document object into a string and write out to the standard output