Serializing DOM object

Discussion in 'Java' started by asd, Jun 8, 2005.

  1. asd

    asd Guest

    Hi all,

    My java application creates an XML DOM object.
    Does anybody know how to write this object into a file?

    Please help me out.

    regards,

    ASD
     
    asd, Jun 8, 2005
    #1
    1. Advertising

  2. On 8 Jun 2005 02:38:44 -0700, asd wrote:

    > My java application creates an XML DOM object.
    > Does anybody know how to write this object into a file?


    Beginner questions are best asked on c.l.j.help
    <http://www.physci.org/codes/javafaq.jsp#cljh>
    ...but please read this first..
    <http://java.sun.com/docs/books/tutorial/essential/io/>

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jun 8, 2005
    #2
    1. Advertising

  3. asd

    Guest

    I would first convert the XML Document into a String, then write the
    String to a file. The first part would be like...

    // may not need all these but at least a few
    import java.io.*;
    import java.util.*;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.dom.DOMSource;
    import org.xml.sax.InputSource;

    public static final String getDocumentAsString(Document document)
    throws XMLHelperException
    {
    try
    {
    // Create source and result objects
    Source source = new DOMSource(document);
    StringWriter out = new StringWriter();
    Result result = new StreamResult(out);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);
    return out.toString();
    }
    catch(Exception e)
    {
    throw new XMLHelperException("XML Document to String Error", e);
    }
    }

    >From there google on writing a String to a new file.


    HTH,
    iksrazal
    http://www.braziloutsource.com/
     
    , Jun 8, 2005
    #3
  4. asd

    asd Guest

    Thanks to you all for all the help.
     
    asd, Jun 13, 2005
    #4
    1. Advertising

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. Natan
    Replies:
    5
    Views:
    1,637
    Natan
    Sep 27, 2004
  2. learningjava

    de/serializing a graphics object

    learningjava, Jan 29, 2004, in forum: Java
    Replies:
    2
    Views:
    336
    learningjava
    Jan 30, 2004
  3. Replies:
    0
    Views:
    581
  4. Tjerk Wolterink

    Serializing an XML Dom

    Tjerk Wolterink, Dec 1, 2005, in forum: XML
    Replies:
    1
    Views:
    709
    Martin Honnen
    Dec 2, 2005
  5. Bla
    Replies:
    1
    Views:
    624
    Lothar Kimmeringer
    Sep 30, 2006
Loading...

Share This Page