any easy way to write out a XML DOM object to file?

Discussion in 'Java' started by Kaidi, Nov 25, 2004.

  1. Kaidi

    Kaidi Guest

    Hello!

    I am using Document and DOM to hold a XML file, and make some changes
    to the Document. After that, I just wonder is there any easy way to
    write the modified XML file back to disk? (loop all the nodes and
    print them out is one way of course, any other better way?)

    The piece of node is:

    String filename="mytest.xml";
    DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
    // Create the builder and parse the file
    Document doc = factory.newDocumentBuilder().parse(new
    File(filename));
    // changes the doc, add, edit nodes, etc.

    Now, I want to get the updated xml file from "doc", what to do?
    Anything like doc.writeAsXMLFile("newFile.xml")?

    Thanks and happy holiday. :)
     
    Kaidi, Nov 25, 2004
    #1
    1. Advertisements

  2. Kaidi

    Alex Kizub Guest

    >
    > Now, I want to get the updated xml file from "doc", what to do?
    > Anything like doc.writeAsXMLFile("newFile.xml")?
    >


    OutputFormat format = new OutputFormat(doc);
    Writer writer = new FileWriter("newFile.xml");
    XMLSerializer ser = new XMLSerializer(writer, format);
    ser.serialize(doc);
    writer.close();

    Alex Kizub.
     
    Alex Kizub, Nov 25, 2004
    #2
    1. Advertisements

  3. Hello,

    XMLSerializer is deprecated (old xerces).
    Some code I use to transform a Document into a String:

    Document document = ...;
    DOMSource domSource = new DOMSource(document);
    StreamResult streamResult = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
    // serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
    serializer.setOutputProperty(OutputKeys.INDENT,"yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
    serializer.transform(domSource, streamResult);
    String result = writer.getBuffer().toString();

    Best regards,
    feri
     
    Ferenc Hechler, Nov 26, 2004
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C-man
    Replies:
    9
    Views:
    860
    Roedy Green
    Apr 11, 2004
  2. Replies:
    0
    Views:
    707
  3. Jim Mitten
    Replies:
    3
    Views:
    583
    Joseph Kesselman
    Jan 29, 2008
  4. Mike Barnard
    Replies:
    5
    Views:
    3,732
    Toby A Inkster
    Mar 27, 2008
  5. srinivasan srinivas

    Is there any way to find out sizeof an object

    srinivasan srinivas, Jun 24, 2008, in forum: Python
    Replies:
    3
    Views:
    361
    zooko
    Jun 26, 2008
  6. RayLopez99
    Replies:
    16
    Views:
    5,364
    RayLopez99
    Aug 4, 2009
  7. Raymond Schanks
    Replies:
    0
    Views:
    475
    Raymond Schanks
    Jul 15, 2010
  8. hackingKK
    Replies:
    1
    Views:
    457
    Diez B. Roggisch
    Oct 7, 2010
Loading...