How to write XML declaration with DOMWriter class Xerces-c

Discussion in 'XML' started by christian.eickhoff@nt.uni-saarland.de, Mar 5, 2007.

  1. -saarland.de

    -saarland.de Guest

    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(gLS);
    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
     
    -saarland.de, Mar 5, 2007
    #1
    1. Advertising

  2. -saarland.de

    spiff Guest

    On 5 Mrz., 11:14, -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
     
    spiff, Mar 5, 2007
    #2
    1. Advertising

  3. -saarland.de

    -saarland.de Guest

    On 5 Mrz., 14:15, "spiff" <> wrote:
    > On 5 Mrz., 11:14, -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
     
    -saarland.de, Mar 5, 2007
    #3
  4. -saarland.de

    spiff Guest

    On 5 Mrz., 16:02, -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
     
    spiff, Mar 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon
    Replies:
    0
    Views:
    1,352
    Simon
    Jul 3, 2003
  2. cvissy
    Replies:
    0
    Views:
    623
    cvissy
    Nov 16, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    527
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,270
    Richard Herring
    May 17, 2005
  5. D McGilvray

    Xerces: Changing dtd declaration

    D McGilvray, Aug 25, 2006, in forum: XML
    Replies:
    1
    Views:
    345
    D McGilvray
    Aug 25, 2006
Loading...

Share This Page