Search code examples
c++xercesxerces-c

Generate XML using Xerces-C++


I am attempting to generate XML similar to the below using the xerces libraries. I cannot find a suitable example to follow; can anyone with experience in this area please advise?

<ad xsi:noNamespaceSchemaLocation="smaato_ad_v0.9.xsd" modelVersion="0.9">
    <richmediaAd>
        <content>
            <script>yadda...yadda... richmedia content ...yadda</script>
        </content>
        <width>728</width>
        <height>90</height>
        <beacons>
            <beacon>http://mysite.com/beacons/mybeacon1</beacon>
            <beacon>http://mysite.com/beacons/mybeacon2</beacon>
        </beacons>
    </richmediaAd>
 </ad>

Solution

  • Replace the creation of the document in the code of the Codeproject sample with

    p_DOMDocument = p_DOMImplementation->createDocument(0, L"ad", 0);
    

    to create a document with an ad element as root node.

    Access the root element in the document with

    DOMElement* pRoot = p_DOMDocument->getDocumentElement();
    

    Create single elements with calls like:

    DOMElement* pEle = p_DOMDocument->createElement(L"richmediaAd");
    pRoot->appendChild(pEle);
    

    Set attributes with calls to

    pEle->setAttribute(L"modelVersion", L"0.9");
    

    Set textual content like this:

    DOMText* pText = p_DOMDocument->createTextNode(L"yadda...yadda...");
    pEle->appendChild(pText);
    

    Hope this helps