开发者

Adding XML elements and attributes

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:(Insid开发者_运维知识库e 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 >


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜