IXMLDOMDocumentPtr pDoc; IXMLDOMNodePtr pXmlInstruction; HRESULT hr = pDoc.CreateInstance("Microsoft.XMLDOM"); CComVariant vFileName("ReadXML.xml"); CComVariant cComVari; VARIANT_BOOL vb; hr = pDoc->load(vFileName,&vb); BSTR bstrInfo; BSTR bstrInfo2; VARIANT vInfo; //文档节点 pDoc->get_nodeName(&bstrInfo); //#document SysFreeString(bstrInfo); pDoc->get_nodeValue(&vInfo); //NULL //第一个节点都是通过 document 的 get_firstChild 来获取 hr = pDoc->get_firstChild(&pXmlInstruction); #if 0 //<?xml version="1.0" encoding="gb2312"?> pXmlInstruction->get_nodeName(&bstrInfo); //xml SysFreeString(bstrInfo); pXmlInstruction->get_nodeValue(&vInfo); //version="1.0" encoding="gb2312" pXmlInstruction->get_text(&bstrInfo); //version="1.0" encoding="gb2312" SysFreeString(bstrInfo); pXmlInstruction->get_dataType(&vInfo); //null pXmlInstruction->get_nodeTypeString(&bstrInfo);//processinginstruction SysFreeString(bstrInfo); #endif IXMLDOMNodePtr pComment1; pXmlInstruction->get_nextSibling(&pComment1); #if 0 //注释操作 //<!-- Copyright w3school.com.cn --> pComment1->get_nodeName(&bstrInfo); //#comment SysFreeString(bstrInfo); pComment1->get_nodeValue(&vInfo); // Copyright w3school.com.cn pComment1->get_text(&bstrInfo); // Copyright w3school.com.cn SysFreeString(bstrInfo); pComment1->get_nodeTypeString(&bstrInfo); //comment SysFreeString(bstrInfo); //cComVari = "注释修改"; //pComment1->put_nodeValue(cComVari); //ok bstrInfo = _com_util::ConvertStringToBSTR("注释修改123456"); pComment1->put_text(bstrInfo); SysFreeString(bstrInfo); #endif IXMLDOMNodePtr pStoreNode; #if 0 //storelist节点 IXMLDOMNodeListPtr pStoreList; bstrInfo = _com_util::ConvertStringToBSTR("bookstore"); hr = pDoc->getElementsByTagName(bstrInfo,&pStoreList); SysFreeString(bstrInfo); long lCount; hr = pStoreList->get_length(&lCount); //1 IXMLDOMNodePtr pNextNode; hr = pStoreList->get_item(0,&pNextNode); pNextNode->get_nodeName(&bstrInfo); //bookstore pNextNode->get_nodeValue(&vInfo); //null //所有文本:Harry Potter J K. Rowling 2005 29.99 //Everyday Italian Giada De Laurentiis 2005 30.00 //Learning XML Erik T. Ray 2003 39.95 //XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 pNextNode->get_text(&bstrInfo2); SysFreeString(bstrInfo); SysFreeString(bstrInfo2); #endif #if 0 //通过 getElementsByTagName 来访问元素 IXMLDOMNodeListPtr pTitleList; bstrInfo = _com_util::ConvertStringToBSTR("title"); hr = pDoc->getElementsByTagName(bstrInfo,&pTitleList); SysFreeString(bstrInfo); long lCount; hr = pTitleList->get_length(&lCount); //4 IXMLDOMNodePtr pListElement; for(long lLoop = 0; lLoop < lCount; lLoop++) { pTitleList->get_item(lLoop,&pListElement); //访问属性 IXMLDOMNamedNodeMapPtr pAttriNodeMap; long lAttriListLength; pListElement->get_attributes(&pAttriNodeMap); pAttriNodeMap->get_length(&lAttriListLength);//[0]:1 for(long lAttriLoop = 0; lAttriLoop < lAttriListLength; lAttriLoop++) { IXMLDOMNodePtr pNode; pAttriNodeMap->get_item(lAttriLoop,&pNode); pNode->get_nodeName(&bstrInfo);//[0]:lang1 [1]:lang2 [2]:lang3 [3]:lang4 pNode->get_nodeValue(&vInfo);//[0]:en1 [1]:en2 [2]:en3 [3]:en4 pNode->get_text(&bstrInfo2);//[0]:en1 [1]:en2 [2]:en3 [3]:en4 SysFreeString(bstrInfo); SysFreeString(bstrInfo2); pNode->get_nodeTypeString(&bstrInfo);//[0]:attribute SysFreeString(bstrInfo); } pListElement->get_nodeValue(&vInfo);//null pListElement->get_text(&bstrInfo);//[0]:Harry Potter [1]:Everyday Italian [2]:Learning XML [3]:XQuery Kick Start pListElement->get_nodeTypeString(&bstrInfo2);//element SysFreeString(bstrInfo); SysFreeString(bstrInfo2); } #endif #if 0//修改节点 IXMLDOMNodeListPtr pAuthorList; bstrInfo = _com_util::ConvertStringToBSTR("author"); pDoc->getElementsByTagName(bstrInfo,&pAuthorList); SysFreeString(bstrInfo); long lAuthorNumber; pAuthorList->get_length(&lAuthorNumber); IXMLDOMNodePtr pAuthorNode; pAuthorList->get_item(lAuthorNumber - 1,&pAuthorNode); #if 1 //通过 pAuthorNode 和 pChildOfAuthor 都可以成功修改 author 内容 //pAuthorNode 是元素节点 pChildOfAuthor则是文本节点 pAuthorNode->get_nodeName(&bstrInfo2);//author pAuthorNode->get_nodeTypeString(&bstrInfo);//element pAuthorNode->get_nodeValue(&vInfo);//null SysFreeString(bstrInfo); SysFreeString(bstrInfo2); bstrInfo = _com_util::ConvertStringToBSTR("test"); pAuthorNode->put_text(bstrInfo); //可以成功修改 SysFreeString(bstrInfo); #else IXMLDOMNodePtr pChildOfAuthor; pAuthorNode->get_firstChild(&pChildOfAuthor); pChildOfAuthor->get_nodeName(&bstrInfo2);//#text pChildOfAuthor->get_nodeTypeString(&bstrInfo);//text pChildOfAuthor->get_nodeValue(&vInfo);//Vaidyanathan Nagarajan SysFreeString(bstrInfo); SysFreeString(bstrInfo2); bstrInfo = _com_util::ConvertStringToBSTR("shenzhen"); pChildOfAuthor->put_text(bstrInfo); SysFreeString(bstrInfo); #endif #endif #if 1//修改属性 IXMLDOMNodeListPtr pBookList; bstrInfo = _com_util::ConvertStringToBSTR("book"); pDoc->getElementsByTagName(bstrInfo,&pBookList); SysFreeString(bstrInfo); long lBookNum; pBookList->get_length(&lBookNum); for(long lLoop = 0; lLoop < lBookNum; lLoop++) { IXMLDOMNodePtr pBookNode; pBookList->get_item(lLoop,&pBookNode); IXMLDOMNamedNodeMapPtr pAttriMap; pBookNode->get_attributes(&pAttriMap); long lAttriNum; pAttriMap->get_length(&lAttriNum); for(long lAttriLoop = 0; lAttriLoop < lAttriNum; lAttriLoop++) { IXMLDOMNodePtr pElementOfAttriMap; pAttriMap->get_item(lAttriLoop,&pElementOfAttriMap); pElementOfAttriMap->get_nodeName(&bstrInfo); SysFreeString(bstrInfo); hr = pElementOfAttriMap->get_nodeValue(&vInfo); char *pszAttri = _com_util::ConvertBSTRToString(vInfo.bstrVal); CString strAttri(pszAttri); strAttri += "'s book"; vInfo.bstrVal = strAttri.AllocSysString(); delete []pszAttri; hr = pElementOfAttriMap->put_nodeValue(vInfo); } //IXMLDOMNodePtr pChildOfBook; //pBookNode->get_firstChild(&pChildOfBook);//获取到title子节点,与要获取的属性不符 //pChildOfBook->get_nodeName(&bstrInfo);//获取到title子节点,与要获取的属性不符 //SysFreeString(bstrInfo); } #endif cComVari = "D:\\Other\\XX_学习资料\\IT\\MSXML\\new.xml"; pDoc->save(cComVari);
对应xml:
<?xml version="1.0" encoding="gb2312"?> <!-- Copyright w3school.com.cn --> <bookstore> <book category="children"> <title lang1="en1">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="cooking"> <title lang2="en2">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web" cover="paperback"> <title lang3="en3">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> <book category="web"> <title lang4="en4">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> </bookstore>
原文链接:https://www.f2er.com/xml/298539.html