/**
* section: Tree
* synopsis: Navigates a tree to print element names
* purpose: Parse a file to a tree, use xmlDocGetRootElement() to
* get the root element, then walk the document and print
* all the element name in document order.
* usage: tree1 filename_or_URL
* test: tree1 test2.xml > tree1.tmp && diff tree1.tmp $(srcdir)/tree1.res
* author: Dodji Seketeli
* copy: see Copyright for the status of this software.
*/
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h> /*
*To compile this file using gcc you can type
* gcc `xml2-config --cflags --libs` -o tree1 tree1.c
*Run this program
* ./tree1 test.xml
*/ static const char* LEVELS[] = {"", " ", " ", " ", " ", " ", " ", " " }; static void printTree(xmlNode * a_node, int level); /**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* 打印所有兄弟节点和子节点的名字.
*/
static void print_element_names(xmlNode * a_node, const char* msg); // 根据标签名称获取节点(可以实现更加复杂的逻辑,获取指定节点)
static xmlNode *getNode(xmlNode *rootNode, const char* tag, xmlNode **parentNode); // 删除当前节点,但是保留子节点
static void removeNode(xmlNode *parentNode, xmlNode *nodeToDelete); // 用一个父节点包装子节点
static void wrapWithNode(xmlNode *parentNode, xmlNode *node, xmlNode *newNode); // 增加一个新节点
static void appendNewChildNode(xmlNode *parentNode, xmlNode *newNode); /**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* Prints the names of the all the xml elements
* that are siblings or children of a given xml node.
*/
static int test_removeNode(const char* filepath); /**
* print_element_names:
* @a_node: the initial xml node to consider.
*
* Prints the names of the all the xml elements
* that are siblings or children of a given xml node.
*/
static int test_wrapWithNode(const char* filepath); int main(int argc, char **argv)
{
if (argc != ) {
printf("error: invalid arguments");
return -;
} /*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION printf("test: removeNode:\n");
test_removeNode(argv[]); printf("\n\ntest: wrapWithNode\n");
test_wrapWithNode(argv[]); /*
*Free the global variables that may
*have been allocated by the parser.
*/
xmlCleanupParser(); return ;
} void print_element_names(xmlNode * a_node, const char* msg)
{
xmlNode *cur_node = NULL; if (msg != NULL && strlen(msg) > ) {
printf("print: %s\n", msg);
} for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("node type: Element, name: %s\n", cur_node->name);
} print_element_names(cur_node->children, "");
}
} void printTree(xmlNode * a_node, int level)
{
xmlNode *cur_node = NULL; //printf("%s", LEVELS[level]); for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
printf("%s%s <%d>\n", LEVELS[level], cur_node->name, cur_node->type);
printTree(cur_node->children, level + );
} else {
printf("%s#%s <%d>\n", LEVELS[level], cur_node->name, cur_node->type);
}
}
} xmlNode *getNode(xmlNode *rootNode, const char* tag, xmlNode **parentNode) {
xmlNode *cur = rootNode;
if ((cur->type == XML_ELEMENT_NODE) && (!xmlStrcmp(cur->name, (const xmlChar *)tag))){
*parentNode = NULL;
return cur;
} *parentNode = cur;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((cur->type == XML_ELEMENT_NODE) && (!xmlStrcmp(cur->name, (const xmlChar *)tag))){
return cur;
} if (cur->type == XML_ELEMENT_NODE) {
*parentNode = cur;
}
cur = cur->next;
} return NULL;
} // 删除当前节点,但是保留子节点
void removeNode(xmlNode *parentNode, xmlNode *nodeToDelete) {
if (nodeToDelete == NULL) {
printf("error: nodeToDelete is null");
return;
} xmlNodePtr siblingNode = nodeToDelete->next; while (siblingNode != NULL) {
if (siblingNode->type == XML_ELEMENT_NODE) {
printf("debug: found sibling: %s\n", siblingNode->name);
break;
} siblingNode = siblingNode->next;
} printf("debug: parentNode: %s, nodeToDelete: %s\n", parentNode->name, nodeToDelete->name);
printTree(parentNode, ); xmlNode *childrenNode = nodeToDelete->children;
if (childrenNode == NULL) {
printf("warn: childrenNode is null\n");
}
//xmlUnlinkNode(nodeToDelete->children); xmlNodePtr nextChildNode = NULL; while (childrenNode != NULL) {
printf("debug: childrenNode: %s\n", childrenNode->name);
nextChildNode = childrenNode->next;
xmlUnlinkNode(childrenNode); if (siblingNode != NULL) {
printf("debug: addPreSibling: %s, sibling is %s\n", childrenNode->name, siblingNode->name);
xmlAddPrevSibling(siblingNode, nextChildNode);
} else {
printf("debug: addChild: %s, parent is %s\n", childrenNode->name, parentNode->name);
printTree(childrenNode, );
xmlAddChild(parentNode, childrenNode);
} childrenNode = nextChildNode;
} xmlUnlinkNode(nodeToDelete);
xmlFreeNode(nodeToDelete);
} // 用一个父节点包装子节点
void wrapWithNode(xmlNode *parentNode, xmlNode *node, xmlNode *newNode) {
xmlUnlinkNode(node);
xmlAddChild(newNode, node);
xmlAddChild(parentNode, newNode);
} // 增加一个新节点
void appendNewChildNode(xmlNode *parentNode, xmlNode *newNode) {
xmlAddChild(parentNode, newNode);
} int test_removeNode(const char* filepath) {
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
xmlNode *parentNode = NULL;
xmlNode *curNode = NULL; /*parse the file and get the DOM */
doc = xmlReadFile(filepath, NULL, ); if (doc == NULL) {
printf("error: could not parse file %s\n", filepath);
} /*Get the root element node */
root_element = xmlDocGetRootElement(doc); // 删除节点,但是保留子节点
curNode = getNode(root_element, "p", &parentNode);
if (curNode == NULL) {
printf("error: p node is not found");
return -;
}
if (parentNode == NULL) {
// 根节点只能有一个子节点,这里就不处理了
printf("error: This is root node, should treat specially. root node should have only one node");
return -;
}
removeNode(parentNode, curNode); // 重新获取跟节点,应该是main了
root_element = xmlDocGetRootElement(doc); print_element_names(root_element, "after delete"); /*free the document */
xmlFreeDoc(doc);
return ;
} int test_wrapWithNode(const char* filepath) {
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
xmlNode *newNode = NULL; /*parse the file and get the DOM */
doc = xmlReadFile(filepath, NULL, ); if (doc == NULL) {
printf("error: could not parse file %s\n", filepath);
} /*Get the root element node */
root_element = xmlDocGetRootElement(doc); // 增加一个父节点,根节点需要特殊处理
xmlUnlinkNode(root_element);
newNode = xmlNewNode(NULL, BAD_CAST "main");
xmlAddChild(newNode, root_element);
xmlDocSetRootElement(doc, newNode);
// 重新获取跟节点,应该是main了
root_element = xmlDocGetRootElement(doc); print_element_names(root_element, "after wrap"); /*free the document */
xmlFreeDoc(doc); return ;
}

https://segmentfault.com/q/1010000000581732

libxml的更多相关文章

  1. xcode升级,报错 libxml/tree.h not found (Xcode4.6解决方案)

    转:http://blog.csdn.net/yangxuanlun/article/details/8639075 Xcode升级到4.6以后,他妈的,libxml/tree.h找不到了,搞了大半天 ...

  2. PHP libxml RSHUTDOWN安全限制绕过漏洞(CVE-2012-1171)

    漏洞版本: PHP PHP 5.5.x 漏洞描述: BUGTRAQ ID: 65673 CVE(CAN) ID: CVE-2012-1171 PHP是一种HTML内嵌式的语言. PHP 5.x版本内的 ...

  3. PHP Libxml

    PHP Libxml 函数 PHP:指示支持该函数的最早的 PHP 版本. 函数 描述 PHP libxml_clear_errors() 清空 Libxml 错误缓冲. 5 libxml_get_e ...

  4. libxml两种换行方法

    好久没上来留下一些记录了,可能是太忙,又或者是过于慵懒便疏于整理. libxml是一个开源的库,linux下解析xml文件经常用到,进行一些创读增删的操作. 最开始接触的时候,看到了一个简明易懂的&l ...

  5. 导入libxml.dylib用Google的GDataXML解析XML数据

    1.用Google的GDataXML来解析XML数据,导入libxml.dylib 2.导入libxml.dylib的操作实现,一开始自己总是找不到libxml.dylib文件. 选择其他文件,到路径 ...

  6. PHP Libxml 函数

    PHP Libxml 简介 Libxml 函数和常量与 SimpleXML.XSLT 以及 DOM 函数一起使用. 安装 这些函数需要 Libxml 程序包. 在 xmlsoft.org 下载 PHP ...

  7. TTPRequest 提示#import <libxml/HTMLparser.h>找不到 的解决方法

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/3984251.html ,转载请注明出处. ASIHTTPRequest 或者AFNetwork提示的#impo ...

  8. libxml/HTMLparser.h file

    在导入asihttprequest包时出问题导入了libxml2.dylib,但是却提示libxml/HTMLparser.h file not found,那是因为你的开发环境默认的路径无法找到这个 ...

  9. libxml2.dylb 罗致<libxml/tree.h> 老是找不到头文件

    libxml2.dylb 导致<libxml/tree.h> 老是找不到头文件 添加了libxml2.dylb的framework ,结果还是引用不了<libxml/tree.h&g ...

  10. 使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h"

    使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h",解决方法如下: 1>.在xcode中左边选中项目的root节点,在中间编 ...

随机推荐

  1. Oracle之q操作符

    Oracle本身默认的是单引号,但是在大家写存储过程或者写SQL语句时,有时候需要拼SQL或者是SQL的值里需要传入含单引号的值,此时就需要使用两个单引号''''来进行转义,其实oracle本身提供了 ...

  2. Python BeautifulSoup库的用法

    BeautifulSoup是一个可以从HTML或者XML文件中提取数据的Python库,它通过解析器把文档解析为利于人们理解的文档导航模式,有利于查找和修改文档. BeautifulSoup3目前已经 ...

  3. linux 安装maven,注意下载-bin.tar.gz文件

    先去http://maven.apache.org/download.cgi下载对应的版本然后放到服务器上/var/local文件夹下面, 此处使用的是apache-maven-3.5.2-bin.t ...

  4. SSH Intro - Remove entry

    ssh-keygen -f "/home/frank/.ssh/known_hosts" -R ec2-54-222-218-195.cn-north-1.compute.amaz ...

  5. 安装cloudermanager时出现Acquiring installation lock问题(图文详解)

    不多说,直接上干货! 问题详情 解决办法 哪一个节点被锁就删除哪一个. 解决办法:进入/tmp 目录,ls -a查看,删除scm_prepare_node.*的文件,以及.scm_prepare_no ...

  6. TOJ 3651 确定比赛名次

    描述 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能直接获得每个队的比赛成绩 ...

  7. nginx打开php错误提示

    首先要编辑php配置文件: vi /etc/php.ini error_reporting = E_ERROR display_errors = On  因为我开启了php-fpm.所以,还要编辑 p ...

  8. 九度oj题目1165:字符串匹配

    题目1165:字符串匹配 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3078 解决:1079 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中 ...

  9. django管理界面使用与bootstrap模板使用

    一.bootstrap模板使用 1.去bootstrap官网找一个合适的模板,下载下来,右键另存为即可 bootstrap官网---->bootstrap中文文档3-------->起步- ...

  10. React之特点及常见用法

    1.什么是React? React是一个用于构建用户界面的JavaScript库.主要用于构建UI,很多人认为Reatc是MVC中的V(视图). React起源于Facebook的内部项目,用来架构I ...