#import <msxml4.dll>
void Exit()
{
CoUninitialize();
}
main int (int argc, char * the argv [])
{
the CoInitialize (NULL); // initialize the COM environment
atexit (Exit);
MSXML2::IXMLDOMDocumentPtr pDOMDoc;
pDOMDoc.CreateInstance (__ uuidof (MSXML2 :: DOMDocument40)); // create an object XMLDOMDocument
MSXML2::IXMLDOMProcessingInstructionPtr pDOMPI=pDOMDoc->createProcessingInstruction("xml", "version=\"1.0\""); // 创建XML声明
pDOMDoc-> appendChild (pDOMPI); // add an XML declaration
MSXML2::IXMLDOMElementPtr pDOMRoot;
pDOMDoc-> raw_createElement ((_ bstr_t) "China", & pDOMRoot); // Create [root] node
pDOMRoot-> setAttribute ( "Area", "3600000"); // Set [root] node attribute
pDOMDoc-> appendChild (pDOMRoot ); // add a root node to the Document
MSXML2::IXMLDOMElementPtr pDOMNode;
pDOMDoc-> raw_createElement ((_ bstr_t) "City", & pDOMNode);
pDOMNode-> Puttext ( "Chongqing"); // node assignment
pDOMRoot-> appendChild (pDOMNode); // add a child node to [root] node
pDOMDoc->raw_createElement((_bstr_t)"City", &pDOMNode);
pDOMNode->Puttext("Beijing");
pDOMRoot->appendChild(pDOMNode);
pDOMDoc-> save ( "Test.xml"); // store XML Document
return 0;
}
-------------------------------------------------- --------------------------- present xml based on the read xml
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(L"MSXML2.DOMDocument.6.0");
if(SUCCEEDED(hr))
{
VARIANT_BOOL isSuccessFul;
CComVariant varXmlFile(L"tlacd.xml");
//spXmldoc->put_async(VARIANT_FALSE);
HRESULT hr= spXmldoc->load(varXmlFile, &isSuccessFul);
if(isSuccessFul==VARIANT_TRUE)
{
CComBSTR bstrXml;
CComPtr<IXMLDOMElement> spRoot=NULL;
CComPtr<IXMLDOMElement> spTheBook=NULL;
CComPtr<IXMLDOMElement> spTheElem=NULL;
CComPtr<IXMLDOMNode> spNewNode=NULL;
hr = spXmldoc->get_documentElement(&spRoot);
spRoot->get_xml(&bstrXml);
AfxMessageBox(L"1, 原始的XML");
AfxMessageBox(bstrXml);
spXmldoc->createElement(L"book", &spTheBook);
spXmldoc->createElement(L"name", &spTheElem);
spXmldoc->put_text(L"新书");
spTheBook->appendChild(spTheElem, &spNewNode);
spTheElem.Release();
spNewNode.Release();
spXmldoc->createElement(L"price", &spTheElem);
spTheElem->put_text(L"20");
spTheBook->appendChild(spTheElem, &spNewNode);
The CComPtr <the IXMLDOMNode> spTheNode = NULL;
spRoot-> the selectSingleNode (L "/ Books / Book [name = 'Harry Potter']", & spTheNode);
HR = spTheNode.QueryInterface (& spTheBook);
spTheNode.Release ();
spTheBook- > get_xml (& bstrXml);
AfxMessageBox (L "3," Harry Potter "in XML");
AfxMessageBox (bstrXml);
//// --- this time to modify the price of the book -----
CComPtr <IXMLDOMNodeList > spNodeList = NULL;
the CComPtr <the IXMLDOMNode> SPListItem = NULL;
spTheBook-> get_childNodes (& spNodeList);
spNodeList-> get_Item (. 1, & SPListItem);
spNodeList.Release ();
spListItem->put_text(L"15");
//// want to add an additional attribute --- id, value ---- B01
the CComVariant varId (L "B01");
spTheBook-> the setAttribute (L "ID", varId);
varId.Clear ();
spTheBook-> get_xml (& bstrXml);
spTheBook.Release ();
AfxMessageBox (L ". 4, to" Harry Potter "modifications are complete.");
AfxMessageBox (bstrXml);
//// --- to "Harry Potter" modifications are complete. ----
//// --- use the id attribute to delete "Three Kingdoms" this book ----
spRoot-> the selectSingleNode (L "/ Books / Book [@ id = 'B02']", & spTheNode) ;
HR = spTheNode.QueryInterface (& spTheBook);
spTheNode.Release ();
spTheBook-> get_xml (& bstrXml);
AfxMessageBox (bstrXml);
the CComPtr <the IXMLDOMNode> spParentNode = NULL;
spTheBook-> get_parentNode (& spParentNode);
spParentNode-> the removeChild (spTheBook, & spTheNode);
spTheNode.Release ();
spParentNode.Release ();
spTheBook.Release ();
spRoot -> get_xml (& bstrXml);
AfxMessageBox (L "6, delete" "XML after the" Three Kingdoms);
AfxMessageBox (bstrXml);
//// --- then all book prices below 10 deleted ----
spRoot-> the selectNodes (L "/ Books / Book [. price <10]", & spNodeList);
CComQIPtr <IXMLDOMSelection> spSomeBooks = spNodeList;
spNodeList.Release();
spSomeBooks-> removeAll ();
spSomeBooks.Release ();
spXmldoc-> get_xml (& bstrXml);
AfxMessageBox (L ". 7, the book 10 has been removed is less than the price");
AfxMessageBox (bstrXml);
spRoot.Release ();
bstrXml .Empty ();
// spXmldoc-> the save (varXmlFile); // save xml.
}
VarXmlFile.ClearToZero ();
}
spXmldoc.Release ();
the CoUninitialize ();
Reproduced in: https: //www.cnblogs.com/areliang/archive/2011/11/23/2260566.html