[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

A solution to get SAX InputSource from a DOM Document

  • To: <xml-dev@l...>
  • Subject: A solution to get SAX InputSource from a DOM Document
  • From: "Albert Yu" <albert.yu@i...>
  • Date: Fri, 13 Aug 2004 11:31:04 +0100

dom inputsource
 
Sometimes you need a SAX InputSource from a DOM Document object. For example, if you are using SAX parser to map XML to Java objects, but your source is a DOM Document object.
 
Here is a solution I found, in which the only problem is that it throws a RuntimeException when the transfromation fails:
 
 
public InputStream convert(org.w3c.dom.Document edoc) throws IOException {
 
        final org.w3c.dom.Document doc = edoc;
        final PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream pis = new PipedInputStream();
        pis.connect(pos);
 
        (new Thread(new Runnable() {
 
            public void run() {
                try {
                    transform(doc, pos);
                }
                catch (TransformerException _ex) {
                    throw new RuntimeException("Failed to tranform org.w3c.dom.Document to PipedOutputStream", _ex);
                }
                finally {
                    try {
                        pos.close();
                    }
                    catch (IOException e) {
 
                    }
                }
            }
        }, "MyClassName.convert(org.w3c.dom.Document edoc)")).start();
 
        return pis;
    }
 
 
 private void transform(org.w3c.dom.Document edoc, PipedOutputStream pos) throws TransformerException {
 
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
 
            transformer.setOutputProperty("encoding", "ISO-8859-1");
            transformer.setOutputProperty("indent", "yes");
 
            transformer.transform(new DOMSource(edoc), new StreamResult(pos));
 
}
 
 
 

Best regards,
 

--
Albert Yu
( Yu Yizhuan )
Software Engineer

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.