利用C libxml2的库。需要删除掉xml中某些节点,但我又不想从根节点开始去找。想到了用xpath来找xml的节点,然后参考官方的XPath example(xpath2.c)附上链接http://www.xmlsoft.org/examples/index.html。官方例子只有set node的,没有delete的。改了一下发现能用,用valgrind工具检查内存也通过了。
#include
#include
#include
#include
#include
以下是xml文件test.xml
我要删除所有的child2节点包括它的子节点
discarded
change second discarded text
11
too
11
11
11
11
11
运行后的xml文件
删除了child2节点以及它们的子节点
discarded
change second discarded text
too
通过valgrind检测内存泄漏情况,0 error
==5342== Memcheck, a memory error detector
==5342== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5342== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==5342== Command: ./xmldemo /localdisk/view_store/cmakedemo/src/test.xml
==5342== Parent PID: 3024
==5342==
==5342==
==5342== HEAP SUMMARY:
==5342== in use at exit: 0 bytes in 0 blocks
==5342== total heap usage: 173 allocs, 173 frees, 79,143 bytes allocated
==5342==
==5342== All heap blocks were freed -- no leaks are possible
==5342==
==5342== For lists of detected and suppressed errors, rerun with: -s
==5342== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
参考博客:
https://blog.csdn.net/infoworld/article/details/8223927