3

I have the following XML structure so that I can add my configuration details to lastconnected element:

<?xml version="1.0" encoding="utf-8" ?>
<lastconnectedServers>
</lastconnectedServers >

Now I want to do some XML operation like adding elements and attributes.For Example I want to add the elements to xml above:(Inside elemet lastconnectedServers):

<Server ip="" domain="">
        <SharedFolder name="" type=""/>
        <SharedFolder name="" type =""/>
        <SharedFolder name="" type =""/>
</Server> 

so that the resulting XML will look something like below:

<?xml version="1.0" encoding="utf-8" ?>
<lastconnectedServers>
   <Server ip="" domain="">
        <SharedFolder name="" type=""/>
        <SharedFolder name="" type =""/>
        <SharedFolder name="" type =""/>
   </Server>
</lastconnectedServers >
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • when posting a question like this you should at least indicate you've already tried rather than just waiting for someone to write all the code for you. – OJ. Nov 21 '10 at 11:12
  • @ OJ, I have tried with XMLLite which doesn't use append any where. Was completely unaware if MSXML was using output param and there were absolutely no samples and thus there were no valid code to show – Simsons Nov 22 '10 at 11:25

2 Answers2

4

Here is the sample code using MSXML. COM error checking is omitted. for The code looks a little bit wordy because of ATL helpers for COM usage, but the programming model follows W3C DOM APIs which is quite well accepted by xml developers.

CComPtr<IXMLDOMDocument2> spDoc;
CComPtr<IXMLDOMElement> spServerElement, spSharedFolderElement;
CComPtr<IXMLDOMNode> spServerNode, spLastConnectedServerNode;
IXMLDOMNode* pInsertedNode;
VARIANT_BOOL varSucc;
CComBSTR bstrLastConnected = L"<?xml version=\"1.0\" encoding=\"utf-8\" ?> \
                               <lastconnectedServers> \
                               </lastconnectedServers >";

spDoc.CoCreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER);
spDoc->put_async(VARIANT_FALSE);
spDoc->loadXML(bstrLastConnected, &varSucc);

// Finds the lastConnectedServerNode node with XPath.
spDoc->selectSingleNode(CComBSTR(L"/lastconnectedServers"),
    &spLastConnectedServerNode);

// Creates and appends Server node.
spDoc->createElement(CComBSTR(L"Server"), &spServerElement);
spServerElement->setAttribute(CComBSTR(L"ip"), CComVariant(L""));
spServerElement->setAttribute(CComBSTR(L"domain"), CComVariant(L""));
spLastConnectedServerNode->appendChild(spServerElement, &pInsertedNode);

// Creates and appends the first SharedFolder elements.
spDoc->createElement(CComBSTR(L"SharedFolder"), &spSharedFolderElement);
spSharedFolderElement->setAttribute(CComBSTR(L"name"), CComVariant(L""));
spSharedFolderElement->setAttribute(CComBSTR(L"type"), CComVariant(L""));
spServerElement->appendChild(spSharedFolderElement, &pInsertedNode);

// Creates the second and third SharedFolder elements...

// Gets the xml content.
CComBSTR bstrXml;
spDoc->get_xml(&bstrXml);

wprintf(L"%s", (LPCWSTR) bstrXml);

Hope this helps.

Samuel Zhang
  • 1,290
  • 8
  • 14
3
TiXmlDocument doc("YourFile.xml");
bool loadOkay = doc.LoadFile();

if(loadOkay)
{
    //Variables for XML elements and attributes
    TiXmlElement *pRoot;
    //Get root element
    pRoot = doc.RootElement();
            TiXmlElement * server = new TiXmlElement("Server"); // Create the new child element
            server->LinkEndChild(pRoot);//Links the child to the parent
            server->setAttribute("ip", ""); // Set attributes
            server-setAttribute("domain","");
            foeach(/*Your Data as Value*/)
            {
                TiXmlElement * sharedFolder = new TiXmlElement("SharedFolder");
                server->LinkEndChild(sharedFolder);
                server->setAttribute("name", "");
                server-setAttribute("type","");  
            }


}
if( doc.SaveFile( "YourOutput.xml" ))
{
    return true;
}
else
{
    return false;
}

This should allow you to add new children to a root element and is the basic structure to do it. You can find more information about TinxyXML and how to use it here

BOMEz
  • 1,020
  • 14
  • 34
  • 1
    So... 10 years later. I ran into this question and this answer helped me, but it's not MSXML. Still, I was able to use setAttribute off of a IXMLDOMElement, rather than having to find the full DOMDocument. Thanks! +1 – Richard Aug 26 '20 at 16:42