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();
}
=====================================================