1

I am write some code to update a XML DOM using MSXML4 & C++. I need a method that appends a child element to a parent element. The code I have written below works until the title of the child matches the title of another child under the parent. I cannot change the title of the children so I need to find a way to append them to the parent.

Can anyone provide some guidance?

// this call creates '<parent><child/></parent>'
AppendChild("/root/parent", "child");

// this call attempts to create '<parent><child/><child/></parent>' but the DOM remains     unchanged ('<parent><child/></parent>')
AppendChild("/root/parent", "child");


void AppendChild(const std::string kPathOfParent, const std::string kNameOfChild)
{
    MSXML2::IXMLDOMNodePtr pElement = m_pXmlDoc->createNode(NODE_ELEMENT, kNameOfChild.c_str(), m_xmlns.c_str());

    MSXML2::IXMLDOMNodePtr pParent = m_pXmlDoc->selectSingleNode(kPathOfParent.c_str());
    MSXML2::IXMLDOMNodePtr pNewChild = pParent->appendChild(pElement);
}
TERACytE
  • 7,553
  • 13
  • 75
  • 111
  • Hey, you're passing C-strings where you should be passing BSTRs. All of these COM methods take BSTRs. Please go read this: http://blogs.msdn.com/ericlippert/archive/2003/09/12/52976.aspx – i_am_jorf Oct 28 '09 at 22:30
  • Is appendChild() returning a non-NULL pointer? – Remy Lebeau Oct 29 '09 at 00:09
  • Thanx Jeff about reminding me to switch to BSTR's. I had forgotten to do so. Remy, the pNewChild is not NULL for either of the child elements added. – TERACytE Oct 29 '09 at 03:25

1 Answers1

1

I am not sure exactly what the problem was, but somewhere my binaries were out of step. I rebuilt the entire project via 'Clean Solution' instead of just the 'Build Solution' option. Now both children are created using the code above. It is not clear to me why I was able to step in to the code via the debugger, but the second child was never created until I cleaned the solution.

Jeff & Remy, thank-you for your comments.

TERACytE
  • 7,553
  • 13
  • 75
  • 111