求助:mfc下,如何删除xml文件中指定属性的节点。 10
比如:<DATA><ROWname="111"/><ROWname="222"/></DATA>怎么删除name="111"的那个节点?...
比如:
<DATA>
<ROW name="111"/>
<ROW name="222"/>
</DATA>
怎么删除name="111"的那个节点? 展开
<DATA>
<ROW name="111"/>
<ROW name="222"/>
</DATA>
怎么删除name="111"的那个节点? 展开
1个回答
展开全部
如果用MSXML DOM方式的话
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(L"MSXML2.DOMDocument.6.0");
if(SUCCEEDED(hr))
{
VARIANT_BOOL isSuccessFul;
CComVariant varXmlFile(L"a.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);
spTheElem->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);
spTheElem.Release();
spNewNode.Release();
spXmldoc->createElement(L"memo", &spTheElem);
spTheElem->put_text(L"新书的更好看。");
spTheBook->appendChild(spTheElem, &spNewNode);
spNewNode.Release();
spTheElem.Release();
spRoot->appendChild(spTheBook, &spNewNode);
spNewNode.Release();
spTheBook.Release();
spRoot->get_xml(&bstrXml);
AfxMessageBox(L"2, 新建一本书完成");
AfxMessageBox(bstrXml);
////--- 新建一本书完成 ----
////--- 下面对《哈里波特》做一些修改。 ----
////--- 查询找《哈里波特》----
CComPtr<IXMLDOMNode> spTheNode=NULL;
spRoot->selectSingleNode(L"/books/book[name='哈里波特']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox(L"3,《哈里波特》的XML");
AfxMessageBox(bstrXml);
////--- 此时修改这本书的价格 -----
CComPtr<IXMLDOMNodeList> spNodeList=NULL;
CComPtr<IXMLDOMNode> spListItem=NULL;
spTheBook->get_childNodes(&spNodeList);
spNodeList->get_item(1, &spListItem);
spNodeList.Release();
spListItem->put_text(L"15");
////--- 另外还想加一个属性id,值为B01 ----
CComVariant varId(L"B01");
spTheBook->setAttribute(L"id", varId);
varId.Clear();
spTheBook->get_xml(&bstrXml);
spTheBook.Release();
AfxMessageBox(L"4, 对《哈里波特》修改完成。");
AfxMessageBox(bstrXml);
////--- 对《哈里波特》修改完成。 ----
////--- 要用id属性删除《三国演义》这本书 ----
spRoot->selectSingleNode(L"/books/book[@id='B02']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox(L"5, 《三国演义》的XML");
AfxMessageBox(bstrXml);
CComPtr<IXMLDOMNode> spParentNode=NULL;
spTheBook->get_parentNode(&spParentNode);
spParentNode->removeChild(spTheBook, &spTheNode);
spTheNode.Release();
spParentNode.Release();
spTheBook.Release();
spRoot->get_xml(&bstrXml);
AfxMessageBox(L"6, 删除《三国演义》后的XML");
AfxMessageBox(bstrXml);
////--- 再将所有价格低于10的书删除 ----
spRoot->selectNodes(L"/books/book[price<10]", &spNodeList);
CComQIPtr<IXMLDOMSelection> spSomeBooks=spNodeList;
spNodeList.Release();
spSomeBooks->removeAll();
spSomeBooks.Release();
spXmldoc->get_xml(&bstrXml);
AfxMessageBox(L"7, 已经删除价格低于10的书");
AfxMessageBox(bstrXml);
spRoot.Release();
bstrXml.Empty();
//spXmldoc->save(varXmlFile); //保存xml。
}
varXmlFile.ClearToZero();
}
spXmldoc.Release();
CoUninitialize();
}
摘自此人博客
http://blog.csdn.net/cds27/article/details/5545455
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(L"MSXML2.DOMDocument.6.0");
if(SUCCEEDED(hr))
{
VARIANT_BOOL isSuccessFul;
CComVariant varXmlFile(L"a.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);
spTheElem->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);
spTheElem.Release();
spNewNode.Release();
spXmldoc->createElement(L"memo", &spTheElem);
spTheElem->put_text(L"新书的更好看。");
spTheBook->appendChild(spTheElem, &spNewNode);
spNewNode.Release();
spTheElem.Release();
spRoot->appendChild(spTheBook, &spNewNode);
spNewNode.Release();
spTheBook.Release();
spRoot->get_xml(&bstrXml);
AfxMessageBox(L"2, 新建一本书完成");
AfxMessageBox(bstrXml);
////--- 新建一本书完成 ----
////--- 下面对《哈里波特》做一些修改。 ----
////--- 查询找《哈里波特》----
CComPtr<IXMLDOMNode> spTheNode=NULL;
spRoot->selectSingleNode(L"/books/book[name='哈里波特']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox(L"3,《哈里波特》的XML");
AfxMessageBox(bstrXml);
////--- 此时修改这本书的价格 -----
CComPtr<IXMLDOMNodeList> spNodeList=NULL;
CComPtr<IXMLDOMNode> spListItem=NULL;
spTheBook->get_childNodes(&spNodeList);
spNodeList->get_item(1, &spListItem);
spNodeList.Release();
spListItem->put_text(L"15");
////--- 另外还想加一个属性id,值为B01 ----
CComVariant varId(L"B01");
spTheBook->setAttribute(L"id", varId);
varId.Clear();
spTheBook->get_xml(&bstrXml);
spTheBook.Release();
AfxMessageBox(L"4, 对《哈里波特》修改完成。");
AfxMessageBox(bstrXml);
////--- 对《哈里波特》修改完成。 ----
////--- 要用id属性删除《三国演义》这本书 ----
spRoot->selectSingleNode(L"/books/book[@id='B02']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox(L"5, 《三国演义》的XML");
AfxMessageBox(bstrXml);
CComPtr<IXMLDOMNode> spParentNode=NULL;
spTheBook->get_parentNode(&spParentNode);
spParentNode->removeChild(spTheBook, &spTheNode);
spTheNode.Release();
spParentNode.Release();
spTheBook.Release();
spRoot->get_xml(&bstrXml);
AfxMessageBox(L"6, 删除《三国演义》后的XML");
AfxMessageBox(bstrXml);
////--- 再将所有价格低于10的书删除 ----
spRoot->selectNodes(L"/books/book[price<10]", &spNodeList);
CComQIPtr<IXMLDOMSelection> spSomeBooks=spNodeList;
spNodeList.Release();
spSomeBooks->removeAll();
spSomeBooks.Release();
spXmldoc->get_xml(&bstrXml);
AfxMessageBox(L"7, 已经删除价格低于10的书");
AfxMessageBox(bstrXml);
spRoot.Release();
bstrXml.Empty();
//spXmldoc->save(varXmlFile); //保存xml。
}
varXmlFile.ClearToZero();
}
spXmldoc.Release();
CoUninitialize();
}
摘自此人博客
http://blog.csdn.net/cds27/article/details/5545455
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询