First, note that an element node is a type of node, and can be appended to or otherwise modified like any other node. You code would work the same if you replaced
 

  DOMNode* m_serviceNode = mDoc->createElement(DOMStr::DOMStr("Person"));

 

with

 

  DOMElement* m_serviceNode = mDoc->createElement(DOMStr::DOMStr("Person"));

 
So I'm not quite sure how you're distinguishing 10 elements from 2 nodes. Your sample document has 12 element nodes in it. Perhaps by nodes you mean elements with children. If so, as far as the DOM is concerned, such elements are fundamentally the same as other elements; they just happen to have different types of children. The ContactType element, for instance, has a text node child, while the Contacts node has a single element child (Person). (Actually, if you parsed your sample document, the resulting DOM it would probably also have text nodes for the white space between the <Contacts> and <Person> opening tags and between the </Person> and </Contacts> closing tags.)
 
I think that what you want is to create a <Contacts> element that is the parent of one or more Person elements. You could accomplish this using the same pattern you already have.
 

  DOMElement* contactsNode = mDoc->createElement(DOMStr::DOMStr("Contacts"));

 

  DOMElement* m_serviceNode = mDoc->createElement(DOMStr::DOMStr("Person"));

  contactsNode.appendChild(m_serviceNode);

 

  :

 

  XMLCh* unicodeStr = m_writer->writeToString(*contactsNode);



From: Mannion, Enda [mailto:[EMAIL PROTECTED]
Sent: Friday, June 02, 2006 12:33 PM
To: [email protected]
Subject: RE: Nexted DOM document

For this I presume I have

 

1 Document

10 elements

2 nodes – this is where I am doubtful

 

 

This is what I have already, how can I add another level of nesting

 

 

DOMNode* m_serviceNode = mDoc->createElement(DOMStr::DOMStr("Person"));

           

try

{

      DOMElement* SubmissionIDNode = mDoc->createElement(DOMStr("ContactType"));

      m_serviceNode->appendChild(SubmissionIDNode);

 

 

      DOMElement* submissionTypeNode = mDoc->createElement(DOMStr("FirstName"));

      m_serviceNode->appendChild(submissionTypeNode);

 

 

      DOMElement* submissionSizeNode = mDoc->createElement(DOMStr("LastName"));

      m_serviceNode->appendChild(submissionSizeNode);

 

 

      DOMElement* indexFileSizeNode = mDoc->createElement(DOMStr("Salutation"));

      m_serviceNode->appendChild(indexFileSizeNode);

 

 

      DOMElement* descriptionNode = mDoc->createElement(DOMStr("Title"));

      m_serviceNode->appendChild(descriptionNode);

 

 

      DOMElement* currentStateNode = mDoc->createElement(DOMStr("EmailAddress"));

      m_serviceNode->appendChild(currentStateNode);

 

 

      DOMElement* annotationsNode = mDoc->createElement(DOMStr("TelephoneNumber"));

      m_serviceNode->appendChild(annotationsNode);

 

 

      DOMElement* workflowIDNode = mDoc->createElement(DOMStr("PreferredLanguage"));

      m_serviceNode->appendChild(workflowIDNode);

 

 

      DOMElement* businessNode = mDoc->createElement(DOMStr("Availability"));

      m_serviceNode->appendChild(businessNode);

}

 

catch (...)

{

      std::cout << "Unexpected Exception \n";

      LOGERROR("Exception in WriteMetaDataXML writing XML tags");

      return false;

}

 

//serialize the xml to a string

XMLCh* unicodeStr = m_writer->writeToString(*m_serviceNode);

char* theStr = xercesc::XMLString::transcode(unicodeStr);

     

xercesc::XMLString::release(&unicodeStr);

 

string metaDataFile = "CustInfo.xml";

ofstream metaFile(metaDataFile.c_str());

 

metaFile<<theStr<<'\n';

 

metaFile.close();

 

xercesc::XMLString::release(&theStr);

 

 

 


From: Mannion, Enda
Sent: 02 June 2006 17:12
To: [email protected]
Subject: Nexted DOM document

 

Could anyone give me a simple sample of how to write a simple nested XML doc using xerces C++ DOM to write a doc like this.

 

<Contacts>

    <Person>

      <ContactType>Device Contact</ContactType>

       <FirstName>ivan</FirstName>

      <LastName>osorio</LastName>

      <Salutation></Salutation>

      <Title>admon Sistema Operativo HPUX</Title>

      <EmailAddress>[EMAIL PROTECTED]</EmailAddress>

      <TelephoneNumber>3158560145</TelephoneNumber>

      <PreferredLanguage>en</PreferredLanguage>

      <Availability>7:00am a 7:00 pm</Availability>

     <Property name="prop1" value="prop2" />

    </ Person>

</Contacts>

 

 

 

Enda

 

Reply via email to