libxml2 使用小结
来源:互联网 发布:阿里云购买 编辑:程序博客网 时间:2024/06/12 01:13
libxml2 是用于C/C++的xml编程的,可以解析或者修改xml文件。
xmlNode是xml文档中的节点,是一个struct,位于tree.h
typedef struct _xmlNode xmlNode;
typedef xmlNode *xmlNodePtr;
struct _xmlNode {
void *_private;/* application data */
xmlElementType type; /* type number, must be second ! */
const xmlChar *name; /* the name of the node, or the entity */
struct _xmlNode *children; /* parent->childs link */
struct _xmlNode *last; /* last child link */
struct _xmlNode *parent;/* child->parent link */
struct _xmlNode *next; /* next sibling link */
struct _xmlNode *prev; /* previous sibling link */
struct _xmlDoc *doc;/* the containing document */
/* End of common part */
xmlNs *ns; /* pointer to the associated namespace */
xmlChar *content; /* the content */
struct _xmlAttr *properties;/* properties list */
xmlNs *nsDef; /* namespace definitions on this node */
void *psvi;/* for type/PSVI informations */
unsigned short line; /* line number */
unsigned short extra; /* extra data for XPath/XSLT */
};
可以用到的方法:// 以 XML_PARSE_NOBLANKS | XML_PARSE_NODICT 为参数打开,使文档不会出现空格 和 换行问题
doc = xmlReadFile(szDocName, "utf-8", XML_PARSE_NOBLANKS | XML_PARSE_NODICT); //解析文档
//遍历所有节点
while (NULL != curNode)
{
if (!xmlStrcmp(curNode->name, (const xmlChar*)"family")) { //获取名称为family的节点
setNode = curNode->xmlChildrenNode;
while (NULL != setNode) {
if (!xmlStrcmp(setNode->name, (const xmlChar*)"nameset")) {
//xmlNewTextChild(setNode, NULL, (const xmlChar*)"hello", (const xmlChar*)"word"); // 增加节点
//nameNode = setNode->xmlChildrenNode; // 得到子节点
//xmlUnlinkNode(nameNode); // 删除节点
//xmlFreeNode(nameNode); // 释放节点
nameNode = setNode->xmlChildrenNode;
while (NULL != nameNode) {
if (!xmlStrcmp(nameNode->name, (const xmlChar*)"name")) {
// 得到节点中的content有两种方法,注意要通过本节点的子节点来获得
xmlChar* content = xmlNodeListGetString(doc, nameNode->xmlChildrenNode, 1);
allName[name_num] = content;
name_num++;
printf("nameContent = %s\n", nameNode->xmlChildrenNode->content); // 得到节点中的content
}
nameNode = nameNode->next;
}
}
setNode = setNode->next;
}
}
curNode = curNode->next;
}
//查找属性为name的值的节点
xmlChar* szPropity = xmlGetProp(propNode, (const xmlChar*)"name");
if (!xmlStrcmp((const xmlChar*)szPropity, (const xmlChar*)"WLAN_Console"))
{
xmlAttrPtr setAttrPtr = propNode->properties;
while (NULL != setAttrPtr)
{
xmlSetProp(propNode, (const xmlChar*)"priority", (const xmlChar*)"debug"); //设置属性priority的值
setAttrPtr = setAttrPtr->next;
}
}
//合并文档方法
curNode = xmlDocGetRootElement(doc); // 得到文档的根节点
curNode2 = xmlDocGetRootElement(doc2);
curNode2 = curNode2->xmlChildrenNode;
while (NULL != curNode2) {
if (!xmlStrcmp(curNode2->name, (const xmlChar*)"family")) {
xmlUnlinkNode(curNode2);
xmlAddChild(curNode,curNode2); // 添加节点
curNode2 = curNode2->next;
}
}
//使用Xpath得到节点的集合
xmlDocPtr doc;
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr NodeSet;
doc = xmlReadFile(szDocName, "utf-8", XML_PARSE_NOBLANKS | XML_PARSE_NODICT);
xpathCtx = xmlXPathNewContext(doc);
xpathObj = xmlXPathEvalExpression((const xmlChar*)("/family/set/name"), xpathCtx);
NodeSet = xpathObj->nodesetval;
NodeSet->nodeTab[i] // nodeTab[i] 是该路径所有节点的集合
NodeSet->nodeNr // nodeNr 是节点的总数
//Xpath 可以简化xml的编辑。
- libxml2 使用小结
- Windows环境下libxml2库的使用小结
- Windows环境下libxml2库的使用小结
- libxml2使用
- libxml2使用
- libxml2使用
- 使用libxml2
- ubuntu12.04 安装libxml2 使用libxml2编译
- 使用Libxml2解析xml
- Libxml2使用简介
- Libxml2使用简介
- 如何使用libxml2库?
- 使用Libxml2解析xm
- libxml2 安装使用
- libxml2 安装使用
- XCode 4 使用 libxml2
- iOS中使用libxml2
- libxml2的使用总结
- c++ Templates读书笔记 9-12章
- 软件升级- 私有部署应用程序升级策略
- Android全屏设置
- 分享11个超棒的移动应用(mobile apps)开发解决方案
- 统计某个网站内的一些内容,很不完善。大家不要看了,浪费时间
- libxml2 使用小结
- Ubuntu 11.10 通过配置vnc使windows可以远程桌面,自动启动
- 命令模式(实例四 计算器)
- Flex项目 报错
- YUV4MPEG2
- 替换空格
- YAFFS 文件系统详细介绍
- 运行Tomcat出现the JRE_HOME environment variable is not defined correctly 的解决办法
- 输入10个整数,将它们从小到大排序后输出,并给出现在每个元素在原来序列中的位置。