You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Alain Leblanc <aa...@gmail.com> on 2008/10/29 19:53:48 UTC
Headers when writing output file in xerces-3.0
Hi All,
I just recently upgraded from 2.8 to 3.0. The transition was mostly
smooth except for when I output a document to a file. There used to be
the line
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
by default on top of the file. Now it is not there. I went from using
a DOMWriter to a DOMLSSerializer. I thought that using
config->setParameter(XMLUni::fgDOMXMLDeclaration, true)
would do it, but it didn't do anything. Anyone has any idea?
Thanks,
Alain
Re: Headers when writing output file in xerces-3.0
Posted by Alain Leblanc <aa...@gmail.com>.
Of course. I had forgotten that the DOMDocument class inherited from
the DOMNode class. Everything makes sense now.
Thanks for your help.
a
2008/10/30 Alberto Massari <am...@datadirect.com>:
> Hi Alain,
> the XML header is written when you write the entire document, not when you
> write the DTD and the root element separately. You should use
>
> theSerializer->write(doc, theOutput);
>
> Alberto
>
>
>
> Alain Leblanc wrote:
>>
>> Here's a sample program. It's short enough that I can put it here. I'm
>> not sure the attachment would make it through:
>>
>> =====================================================
>>
>> #include <iostream>
>> #include <xercesc/dom/DOM.hpp>
>> #include <xercesc/util/XMLString.hpp>
>> #include <xercesc/framework/LocalFileFormatTarget.hpp>
>>
>> XERCES_CPP_NAMESPACE_USE
>> using namespace std;
>>
>> // StrXML class copied from Xerces-2.8 distro.
>>
>> class StrXML
>> {
>> public :
>> //
>> -----------------------------------------------------------------------
>> // Constructors and Destructor
>> //
>> -----------------------------------------------------------------------
>> StrXML(const char* const toTranscode)
>> {
>> // Call the private transcoding method
>> fLocalForm = XMLString::transcode(toTranscode);
>> }
>>
>> ~StrXML()
>> {
>> XMLString::release(&fLocalForm);
>> }
>>
>>
>> //
>> -----------------------------------------------------------------------
>> // Getter methods
>> //
>> -----------------------------------------------------------------------
>> const XMLCh* utf16() const
>> {
>> return fLocalForm;
>> }
>> private :
>> //
>> -----------------------------------------------------------------------
>> // Private data members
>> //
>> // fLocalForm
>> // This is the local code page form of the string.
>> //
>> -----------------------------------------------------------------------
>> XMLCh* fLocalForm;
>>
>> };
>>
>>
>> int main (int , char **) {
>> XMLPlatformUtils::Initialize();
>> DOMImplementation *impl =
>> DOMImplementationRegistry::getDOMImplementation(StrXML("").utf16());
>> if (impl == NULL)
>> throw string("Implementation is NULL in create document.");
>>
>> // Create the document with one tag
>> DOMDocument *doc = impl->createDocument(0, StrXML("session").utf16(),
>>
>> impl->createDocumentType(StrXML("dummy").utf16(),
>> 0,
>> 0));
>>
>> DOMLSSerializer *theSerializer =
>> ((DOMImplementationLS*)impl)->createLSSerializer();
>> DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
>>
>> DOMConfiguration *configuration = theSerializer->getDomConfig();
>>
>> // Have a nice output
>> if (configuration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
>> true))
>> configuration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
>>
>> // Add the declaration?
>> if (configuration->canSetParameter(XMLUni::fgDOMXMLDeclaration, true))
>> configuration->setParameter(XMLUni::fgDOMXMLDeclaration, true);
>>
>>
>>
>> LocalFileFormatTarget *myFormTarget =
>> new LocalFileFormatTarget(StrXML("outfile.xml").utf16());
>> theOutput->setByteStream(myFormTarget);
>> if (doc->getDoctype() == NULL) {
>> cerr << "The doc type is null.\n";
>> exit(-1);
>> }
>> theSerializer->write(doc->getDoctype(), theOutput);
>> theSerializer->write(doc->getDocumentElement(), theOutput);
>> theOutput->release();
>> theSerializer->release();
>> XMLPlatformUtils::Terminate();
>>
>> }
>>
>>
>> =====================================================
>>
>>
>>
>
>
Re: Headers when writing output file in xerces-3.0
Posted by Alberto Massari <am...@datadirect.com>.
Hi Alain,
the XML header is written when you write the entire document, not when
you write the DTD and the root element separately. You should use
theSerializer->write(doc, theOutput);
Alberto
Alain Leblanc wrote:
> Here's a sample program. It's short enough that I can put it here. I'm
> not sure the attachment would make it through:
>
> =====================================================
>
> #include <iostream>
> #include <xercesc/dom/DOM.hpp>
> #include <xercesc/util/XMLString.hpp>
> #include <xercesc/framework/LocalFileFormatTarget.hpp>
>
> XERCES_CPP_NAMESPACE_USE
> using namespace std;
>
> // StrXML class copied from Xerces-2.8 distro.
>
> class StrXML
> {
> public :
> // -----------------------------------------------------------------------
> // Constructors and Destructor
> // -----------------------------------------------------------------------
> StrXML(const char* const toTranscode)
> {
> // Call the private transcoding method
> fLocalForm = XMLString::transcode(toTranscode);
> }
>
> ~StrXML()
> {
> XMLString::release(&fLocalForm);
> }
>
>
> // -----------------------------------------------------------------------
> // Getter methods
> // -----------------------------------------------------------------------
> const XMLCh* utf16() const
> {
> return fLocalForm;
> }
> private :
> // -----------------------------------------------------------------------
> // Private data members
> //
> // fLocalForm
> // This is the local code page form of the string.
> // -----------------------------------------------------------------------
> XMLCh* fLocalForm;
>
> };
>
>
> int main (int , char **) {
> XMLPlatformUtils::Initialize();
> DOMImplementation *impl =
> DOMImplementationRegistry::getDOMImplementation(StrXML("").utf16());
> if (impl == NULL)
> throw string("Implementation is NULL in create document.");
>
> // Create the document with one tag
> DOMDocument *doc = impl->createDocument(0, StrXML("session").utf16(),
> impl->createDocumentType(StrXML("dummy").utf16(),
> 0,
> 0));
>
> DOMLSSerializer *theSerializer =
> ((DOMImplementationLS*)impl)->createLSSerializer();
> DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
>
> DOMConfiguration *configuration = theSerializer->getDomConfig();
>
> // Have a nice output
> if (configuration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
> configuration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
>
> // Add the declaration?
> if (configuration->canSetParameter(XMLUni::fgDOMXMLDeclaration, true))
> configuration->setParameter(XMLUni::fgDOMXMLDeclaration, true);
>
>
>
> LocalFileFormatTarget *myFormTarget =
> new LocalFileFormatTarget(StrXML("outfile.xml").utf16());
> theOutput->setByteStream(myFormTarget);
> if (doc->getDoctype() == NULL) {
> cerr << "The doc type is null.\n";
> exit(-1);
> }
> theSerializer->write(doc->getDoctype(), theOutput);
> theSerializer->write(doc->getDocumentElement(), theOutput);
> theOutput->release();
> theSerializer->release();
> XMLPlatformUtils::Terminate();
>
> }
>
>
> =====================================================
>
>
>
Re: Headers when writing output file in xerces-3.0
Posted by Alain Leblanc <aa...@gmail.com>.
Here's a sample program. It's short enough that I can put it here. I'm
not sure the attachment would make it through:
=====================================================
#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
XERCES_CPP_NAMESPACE_USE
using namespace std;
// StrXML class copied from Xerces-2.8 distro.
class StrXML
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
StrXML(const char* const toTranscode)
{
// Call the private transcoding method
fLocalForm = XMLString::transcode(toTranscode);
}
~StrXML()
{
XMLString::release(&fLocalForm);
}
// -----------------------------------------------------------------------
// Getter methods
// -----------------------------------------------------------------------
const XMLCh* utf16() const
{
return fLocalForm;
}
private :
// -----------------------------------------------------------------------
// Private data members
//
// fLocalForm
// This is the local code page form of the string.
// -----------------------------------------------------------------------
XMLCh* fLocalForm;
};
int main (int , char **) {
XMLPlatformUtils::Initialize();
DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(StrXML("").utf16());
if (impl == NULL)
throw string("Implementation is NULL in create document.");
// Create the document with one tag
DOMDocument *doc = impl->createDocument(0, StrXML("session").utf16(),
impl->createDocumentType(StrXML("dummy").utf16(),
0,
0));
DOMLSSerializer *theSerializer =
((DOMImplementationLS*)impl)->createLSSerializer();
DOMLSOutput *theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
DOMConfiguration *configuration = theSerializer->getDomConfig();
// Have a nice output
if (configuration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
configuration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
// Add the declaration?
if (configuration->canSetParameter(XMLUni::fgDOMXMLDeclaration, true))
configuration->setParameter(XMLUni::fgDOMXMLDeclaration, true);
LocalFileFormatTarget *myFormTarget =
new LocalFileFormatTarget(StrXML("outfile.xml").utf16());
theOutput->setByteStream(myFormTarget);
if (doc->getDoctype() == NULL) {
cerr << "The doc type is null.\n";
exit(-1);
}
theSerializer->write(doc->getDoctype(), theOutput);
theSerializer->write(doc->getDocumentElement(), theOutput);
theOutput->release();
theSerializer->release();
XMLPlatformUtils::Terminate();
}
=====================================================
Re: Headers when writing output file in xerces-3.0
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Alain,
Alain Leblanc <aa...@gmail.com> writes:
> There used to be the line
>
> <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
>
> by default on top of the file. Now it is not there. I went from using
> a DOMWriter to a DOMLSSerializer. I thought that using
>
> config->setParameter(XMLUni::fgDOMXMLDeclaration, true)
>
> would do it, but it didn't do anything. Anyone has any idea?
Hm, this works for me even without the call to setParameter().
Can you provide a sample program that demonstrates the missing
XML declaration problem? There must be something special about
the way you do the serialization.
Boris
--
Boris Kolpackov, Code Synthesis Tools http://codesynthesis.com/~boris/blog
Open source XML data binding for C++: http://codesynthesis.com/products/xsd
Mobile/embedded validating XML parsing: http://codesynthesis.com/products/xsde