472,133 Members | 1,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

How to write XML declaration with DOMWriter class Xerces-c

Hello Everyone,

currently I am implementing a solution to write individually created
XML data to a file using c++ and Xerces-c 2.7.0 library. Therefore I
am making use of DOMWriter class which allows me to write the
according DOM tree to a given location using LocalFileFormatTarget().
Everything works fine except that I cant define and set a certain XML
declaration. The tutorial seems to provide an easy way to do this by
applying the setEncoding(), setVersion() or setStandalone() functions
to the according DOMDocument. But even after setting those parameters
as well as enabling the fgDOMXMLDeclaration() feature of the DOMWriter
nothing is written except one empty line at the beginning of the
document. What am I doing wrong???

The according code snippet is given below:

const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(gL S);
DOMWriter* theSerializer = ((DOMImplementationLS*)impl)-
>createDOMWriter();
if (theSerializer->canSetFeature(XMLUni::fgDOMXMLDeclaration,true) )
theSerializer->setFeature(XMLUni::fgDOMXMLDeclaration, true);

DOMDocument* doc;
doc = impl->createDocument(
0, // root element
namespaceURI.
XMLString::transcode("rootnode"),
// root element name
0); // document type
object(DTD).

doc->setEncoding(XMLString::transcode("UTF-8"));
doc->setVersion(XMLString::transcode("1.0"));

DOMNode* node = XMLwalker->getRoot(); //Set
Walker to Root node of the document
XMLFormatTarget *myFormTarget;
myFormTarget = new
LocalFileFormatTarget(written_XML_name.c_str()); //output to file
try{
theSerializer->writeNode(myFormTarget, *node);
}
catch.....

Any Help is appreciated as for my purposes the integration of the xml
declaration is compulsory!

Best regards,
Christian Eickhoff

Mar 5 '07 #1
3 9372
On 5 Mrz., 11:14, christian.eickh...@nt.uni-saarland.de wrote:
DOMNode* node = XMLwalker->getRoot(); //Set
Walker to Root node of the document
XMLFormatTarget *myFormTarget;
myFormTarget = new
LocalFileFormatTarget(written_XML_name.c_str()); //output to file
try{
theSerializer->writeNode(myFormTarget, *node);
Hi Christian!

You get the XML root here with XMLwalker->getRoot(); but not the root
of the DOM tree. DOMDocument is actually the root of your DOM. Try
replacing writeNode(myFormTarget, *node) with writeNode(myFormTarget,
*doc).

Regards spiff
http://www.spycomponents.com

Mar 5 '07 #2
On 5 Mrz., 14:15, "spiff" <s...@gmx.atwrote:
On 5 Mrz., 11:14, christian.eickh...@nt.uni-saarland.de wrote:
DOMNode* node = XMLwalker->getRoot(); //Set
Walker to Root node of the document
XMLFormatTarget *myFormTarget;
myFormTarget = new
LocalFileFormatTarget(written_XML_name.c_str()); //output to file
try{
theSerializer->writeNode(myFormTarget, *node);

Hi Christian!

You get the XML root here with XMLwalker->getRoot(); but not the root
of the DOM tree. DOMDocument is actually the root of your DOM. Try
replacing writeNode(myFormTarget, *node) with writeNode(myFormTarget,
*doc).

Regards spiffhttp://www.spycomponents.com
Hey spiff,

not the first time you helped me out!!! Thanks so much, good to have
versed experts like you! Now everything works fine =))..

With best regards,
Christian Eickhoff

Mar 5 '07 #3
On 5 Mrz., 16:02, christian.eickh...@nt.uni-saarland.de wrote:
not the first time you helped me out!!! Thanks so much, good to have
versed experts like you! Now everything works fine =))..

With best regards,
Christian Eickhoff
Christian,
fine that it is working now :-)

spiff

Mar 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jochen Daum | last post: by
1 post views Thread by Venkat | last post: by
4 posts views Thread by Ovidesvideo | last post: by
2 posts views Thread by flopbucket | last post: by
1 post views Thread by Jeremy S. | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.