You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Andreas Rieder <an...@allianz.at> on 2003/03/17 17:37:51 UTC

Re: Write XML-Document to File

Kern P.,DP ITS,SMA,M wrote:
> Hello everybody,
> 
> i tried the DocumentBuilder example from the samples of Xalan-C.
> 
> Now my DocumentBuilder holds my XML document and i don't know how to write
> this document to a file for testing purposes.
> 
> Can anybody help me please?
> 
> Thanks in advance.
> 
> Cheers
> 
> Pete
> 

1) Create a new save FileFormatTarget

std::auto_ptr<XMLFormatTarget> FileTarget(new 
LocalFileFormatTarget("C:\\Temp\\Test.xml"));

2) Get the Serializer from the Implementation

m_impl = 
DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));

...
DOMWriter* theSerializer = m_impl->createDOMWriter();

3) Write the whole Document to the File

DOMDocument* m_doc;
m_doc = m_impl->createDocument(...);

...
theSerializer->writeNode(FileTarget.get(), *m_doc)

4) Don't forget to catch any possible exceptions!

;-) AnR



Re: Write XML-Document to File

Posted by David N Bertoni/Cambridge/IBM <da...@us.ibm.com>.



No, this is not the way to do things with Xalan, unless you want to go
through the fuss of wrapping Xalan's tree in the Xerces DOM.  Also, this:

   m_impl =
   DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));

is a memory leak, since the string returned from XMLString::transcode()
must be deleted by the caller.

Instead, try this code.  See the SerializeNodeSet sample program for more
details, or grep the code for some of these classes.


   #include <iostream.h>

   #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
   #include <PlatformSupport/XalanStdOutputStream.hpp>

   #include <XMLSupport/FormatterToXML.hpp>
   #include <XMLSupport/FormatterTreeWalker.hpp>

   void
   foo(const XalanDocumentBuilder&  builder)
   {
       XalanStdOutputStream          theStream(cout);
       XalanOutputStreamPrintWriter  thePrintWriter(theStream);

       FormatterToXML       theFormatter(thePrintWriter);
       FormatterTreeWalker  theWalker(theFormatter);

       theWalker.traverse(builder.getDocument());
   }

Dave



                                                                                                                        
                      Andreas Rieder                                                                                    
                      <andreas.rieder@         To:      xalan-c-users@xml.apache.org                                    
                      allianz.at>              cc:      (bcc: David N Bertoni/Cambridge/IBM)                            
                      Sent by: news            Subject: Re: Write XML-Document to File                                  
                      <news@main.gmane                                                                                  
                      .org>                                                                                             
                                                                                                                        
                                                                                                                        
                      03/17/2003 08:37                                                                                  
                      AM                                                                                                
                                                                                                                        



Kern P.,DP ITS,SMA,M wrote:
> Hello everybody,
>
> i tried the DocumentBuilder example from the samples of Xalan-C.
>
> Now my DocumentBuilder holds my XML document and i don't know how to
write
> this document to a file for testing purposes.
>
> Can anybody help me please?
>
> Thanks in advance.
>
> Cheers
>
> Pete
>

1) Create a new save FileFormatTarget

std::auto_ptr<XMLFormatTarget> FileTarget(new
LocalFileFormatTarget("C:\\Temp\\Test.xml"));

2) Get the Serializer from the Implementation

m_impl =
DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));


...
DOMWriter* theSerializer = m_impl->createDOMWriter();

3) Write the whole Document to the File

DOMDocument* m_doc;
m_doc = m_impl->createDocument(...);

...
theSerializer->writeNode(FileTarget.get(), *m_doc)

4) Don't forget to catch any possible exceptions!

;-) AnR