2

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>
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
godzilla
  • 3,005
  • 7
  • 44
  • 60
  • 2
    Do you have any code that shows where you currently are at? – Chris Dargis Jul 25 '12 at 14:55
  • i am following the example here http://www.codeproject.com/Articles/31088/Xerces-for-C-Tutorial-Using-Visual-C, however i am not certain whether it will meet my requirements – godzilla Jul 25 '12 at 14:56
  • 2
    OK, what are your requirements? How is the example failing to meet them? What are you having trouble with? Please edit your question with answers to these questions. – Chris Dargis Jul 25 '12 at 14:58
  • the requirement is to generate xml with the above constraints, including tag – godzilla Jul 25 '12 at 15:00

1 Answers1

7

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

Clemens
  • 1,744
  • 11
  • 20
  • 1
    One last question, i need to set the attribute to the top level - so xsi:noNamespaceSchemaLocation="smaato_ad_v0.9.xsd" modelVersion="0.9" will be with tag ad rather than richmediaad - is this possible with xerces? – godzilla Jul 27 '12 at 15:39