如何删除节点&它的子级在 XML 中

发布于 2024-11-29 06:03:55 字数 949 浏览 6 评论 0原文

我在从 XML 中删除节点时遇到一些问题。

这是我的 XML 的架构,

<?xml version="1.0" encoding="ISO-8859-1"?>
<file>
    <header Description="Lovely Tool"></header>

  <ToolPath>C:\MyDocs\MyTool\</ToolPath>

   <ToolDetails>
    <Name>XML Tool</Name>
    <Description>XML parser</Description>
    <Comments>Good Tool for XML</Comments>
  </ToolDetails>
</file>

我想删除节点 ToolDetails 及其 childs,我使用 MSXML 尝试这样做,但它不起作用,

这是我的代码

   CString childName;
    MSXML2::IXMLDOMNodePtr childPtr = NULL;
    MSXML2::IXMLDOMNodePtr delNode = NULL;
    int i=0;
    MSXML2::IXMLDOMNodeListPtr  pChildNodeListPtr = NULL;

delNode  = m_pRoot->GetchildNodes()->Getitem(index+2);//m_pRoot is the root ptr
childName=(char*)m_ptrDataBlock->nodeName;
HRESULT hr = m_pRoot->removeChild(delNode);

i am facing some problem in Deleting a Node from XML.

Here is the schema of my XML,

<?xml version="1.0" encoding="ISO-8859-1"?>
<file>
    <header Description="Lovely Tool"></header>

  <ToolPath>C:\MyDocs\MyTool\</ToolPath>

   <ToolDetails>
    <Name>XML Tool</Name>
    <Description>XML parser</Description>
    <Comments>Good Tool for XML</Comments>
  </ToolDetails>
</file>

I want to delete the Node ToolDetailsand its childs, i tried like this using MSXML, but its not working,

Here is my code

   CString childName;
    MSXML2::IXMLDOMNodePtr childPtr = NULL;
    MSXML2::IXMLDOMNodePtr delNode = NULL;
    int i=0;
    MSXML2::IXMLDOMNodeListPtr  pChildNodeListPtr = NULL;

delNode  = m_pRoot->GetchildNodes()->Getitem(index+2);//m_pRoot is the root ptr
childName=(char*)m_ptrDataBlock->nodeName;
HRESULT hr = m_pRoot->removeChild(delNode);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-12-06 06:03:55

Getitem(index+2) 只会返回 index + 2th 项的句柄。

要删除节点,您还需要通过调用来分离该项目
Getitem(index)->detach()

Getitem(index+2) will only return the handle for index + 2th item.

for deleting the node you also need to detach the item by calling
Getitem(index)->detach()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文