libxml2如何解析xml格式的字符串
1. xmlParseMemory,字符串转为XML文档
2. xmlDocGetRootElement,获取XML文档根节点
3. xmlStrcmp,比较XML字符串,与strcmp差不多
4. curr = curr->xmlChildrenNode,XML节点指针指向第一个子节点
5. curr = curr->next,XML节点指针指向下一个兄弟节点
6. xmlNodeGetContent,获取XML节点的内容
7. xmlFreeDoc,释放节点,与free差不多
1. 文件操作函数
a) 保存文件
int xmlSaveFile (const char * filename, xmlDocPtr cur)
将一个内存中的文档,保存到一个文件当中。如果编译使用了压缩功能,并且启用了,这个函数会默认使用压缩(压缩也就是忽略文件当中的格式)。如果设定filanem为”-“,那么将会直接输出到 stdout。
filename: |
the filename (or URL) |
cur: |
the document |
Returns: |
the number of bytes written or -1 in case of failure. |
int xmlSaveFileEnc (const char * filename, xmlDocPtr cur, const char * encoding)
将一个文本保存在文件当中,并且按照要求转换到目标字符集,例如:GB2312
filename: |
the filename (or URL) |
cur: |
the document |
encoding: |
the name of an encoding (or NULL) |
Returns: |
the number of bytes written or -1 in case of failure. |
int xmlSaveFileTo (xmlOutputBufferPtr buf, xmlDocPtr cur, const char * encoding)
将文件保存到一个I/O缓存当中。如果这个缓存已经通过 xmlOutputBufferClose() 关闭掉了,那么将失败。
buf: |
an output I/O buffer |
cur: |
the document |
encoding: |
the encoding if any assuming the I/O layer handles the trancoding |
Returns: |
the number of bytes written or -1 in case of failure. |
int xmlSaveFormatFile (const char * filename, xmlDocPtr cur, int format)
格式化的将内存文档保存到一个文件当中,格式设定与 xmlDocDumpFormatMemory()中一样。
filename: |
the filename (or URL) |
cur: |
the document |
format: |
一般都设定为1 |
Returns: |
the number of bytes written or -1 in case of failure. |
int xmlSaveFormatFileEnc (const char * filename, xmlDocPtr cur, const char * encoding, int format)
有格式整理的xmlSaveFileEnc()。一般都采用这个函数进行内存DOC指针的保存工作。
在调用这个函数以前,最好调用如下两个语句,来整理内容:
xmlKeepBlanksDefault(0);
xmlIndentTreeOutput= 1;
filename: |
the filename or URL to output |
cur: |
the document being saved |
encoding: |
the name of the encoding to use or NULL. |
format: |
一般都设定为1 |
Returns: |
the number of bytes written or -1 in case of error. Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called |
int xmlSaveFormatFileTo (xmlOutputBufferPtr buf, xmlDocPtr cur, const char * encoding, int format)
buf: |
an output I/O buffer |
cur: |
the document |
encoding: |
the encoding if any assuming the I/O layer handles the trancoding |
format: |
一般都设定为1 |
Returns: |
the number of bytes written or -1 in case of failure. |
void xmlDocDumpFormatMemory (xmlDocPtr cur,
xmlChar **mem,
int * size,
int format)
通过这个函数建立的xmlChar,需要调用xmlFree进行内存释放。
cur: |
文档指针 |
mem: |
OUT: 内存中的BUFF 指针 |
size: |
OUT: BUFF 长度 |
format: |
一般都设定为1 |
xmlDocPtr xmlParseMemory (const char * buffer, int size)
通过内存中的BUFF,建立一个DOC文档。通过这个函数建立DOC文档以后,这个文档需要使用 xmlFreeDoc函数进行内存释放。
buffer: |
BUFF指针 |
size: |
BUFF中内容的长度 |
Returns: |
新建立的文档指针 |
2. 节点操作函数
a) 复制节点
node: |
要复制的节点 |
extended: |
0:仅仅添加节点名称,没有任何其他内容;1:添加节点所有内容,包括子节点、属性等等,相当于有格式整理的xmlCopyNodeList;2:只添加节点本身内容和其自身属性; |
Returns: |
一个新的节点指针,或者产生错误返回NULL; |
Do a recursivecopy of the node list. Use xmlDocCopyNodeList() if possible to ensure stringinterning.
node: |
the first node in the list. |
Returns: |
a new #xmlNodePtr, or NULL in case of error. |
node: |
the node |
name: |
the attribute name |
Returns: |
the attribute value or NULL if not found. It's up to the caller to free the memory with xmlFree(). |
int xmlStrEqual (const xmlChar * str1, const xmlChar * str2)
判断两个字符串是否相同,比xmlStrcmp的速度要快一点。
str1: |
the first xmlChar * |
str2: |
the second xmlChar * |
Returns: |
1:相同,0:不同 |
int xmlStrcmp (const xmlChar * str1, const xmlChar * str2)
int xmlStrcasecmp (const xmlChar * str1, const xmlChar * str2)
int xmlStrncmp (const xmlChar * str1, const xmlChar * str2, int len)
int xmlStrncasecmp (const xmlChar * str1, const xmlChar * str2, int len)
int xmlUTF8Charcmp (const xmlChar * utf1, const xmlChar * utf2)
utf1: |
pointer to first UTF8 char |
utf2: |
pointer to second UTF8 char |
Returns: |
result of the compare as with xmlStrncmp |
int xmlStrQEqual (const xmlChar * pref, const xmlChar * name, const xmlChar * str)
Check if a QNameis Equal to a given string
pref: |
the prefix of the QName |
name: |
the localname of the QName |
str: |
the second xmlChar * |
Returns: |
1 if they are equal, 0 if they are different |
▫ http://xmlsoft.org/html/index.html
libxml2如何解析xml格式的字符串相关推荐
- java后台解析xml格式字符串
背景: 在调用京东万象的短信接口的时候,它返回的是json格式的字符串,使用的是京东万象的106短信 接口(https://wx.jdcloud.com/market/datas/5/10306) 解 ...
- 安卓解析xml格式字符串
最近实验室的安卓app需要实现发内部邮件的功能. 说白了就是简单的数据库的增删处理. 但是中间的一部分有意思的就是获取收件人的列表的处理. 用户在登录APP的时候,如果验证成功,服务器则把数据的联系人 ...
- 解析xml格式字符串
解析xml格式字符串 由于写接口时,总有人会传一些xml格式的字符串,节点少时可以一一获取,但是如果有大量的子节点时,可以使用反射机制实现实体类接收xml格式字符串数据,只需要保证节点名称和实体类字段 ...
- Java 中解析 xml 格式字符串的数据
解析 xml 格式中的字符串数据 相信有很多小伙伴经历过接口返回的数据是在一段 xml 字符串格式的数据里,那么我们可用什么最快最便捷的方法取到里面的数据勒? 下面这里是一个xml 格式的是实例 这里 ...
- java jdom格式_Java全面解析XML格式串(JDOM解析)
搜索热词 Java全面解析XML格式串(JDOM解析) import java.io.IOException; import java.io.StringReader; import java.uti ...
- iOS开发之解析XML格式数据
XML格式的数据是一种数据的传输格式.因为它方便编写.结构清晰,所以深受程序猿的喜爱,非常多人都喜欢使用XML格式数据传输或者作为程序的配置信息. 如今我将来实如今iOS中解析XML格式数据,语言使用 ...
- Android——网络交互,如何调用接口并且json解析json格式和pull解析xml格式
本菜鸟因为之前一直使用的是公司前辈封装好的类(网络接口调用超级方便有木有)完全没有想过有一天我会自己写,然后今天接触的时候一脸懵逼,然后今天花了一天时间终于研究好了:哈哈哈哈哈 二话不说先上代码: p ...
- 使用WCF传输DataTable:DataTable和Xml格式的字符串相互转换(C#)
背景:项目中要用到客户端向服务端传数据,使用WCF,绑定webHttpBinding,做了一个小例子. 业务逻辑简介:客户端在a表中添加了几条数据,从SQL Server数据库直接取出新添加的数据(D ...
- php获得帮助类数据_PHP解析xml格式数据工具类示例
本文实例讲述了PHP解析xml格式数据工具类.分享给大家供大家参考,具体如下: class ome_xml { /** * xml资源 * * @var resource * @see xml_par ...
最新文章
- 大写的服!90 后程序员利用业余时间帮网友鉴定毒蘑菇,拥有百万粉丝成网络大 V...
- linux apt 命令,Ubuntu系统中apt命令的用法汇总
- mysql生成uui mybatis_mybatis----基础
- Xcode LaunchImage 载入界面大小设置
- C++三大继承与多级派生
- 大数据开发笔记(八):Spark综合笔记总结
- C++最小函数模板demo
- 一招判断三元催化堵塞_三元催化堵塞,许多老司机不知道如何处理,教你一妙招,油耗低...
- PTA程序设计基础题目集(1)
- 用slub track调试use after free问题
- 【PMI-PMP®模考三】2022
- 华为手机像素密度排行_华为荣耀20怎么调整像素密度,如何提升屏显清晰度呢...
- ZigBee Dotdot
- DS90UB953-Q1-DS90UB954-Q1调试记录
- 学习笔记(29):A110测试-测试课程申请22
- 防刷新网站访问量计数器
- WPA_CLI 的介绍:介绍如何使用wpa_cli连接WiFi的方法
- 【数字逻辑与数字系统设计】应对期末考(更新于2022/4/15)
- 面向对象的特点?对象模型、动态模型和功能模型3种模型之间的关系?
- MongoDB基础教程
热门文章
- 在定义SharePoint列表的SPD数据视图的时候需要注意的问题
- 第二十二章 5为你的命名空间取个别名
- 在Linux服务器之间迁移帐号信息
- 牛客 - Elo mountains(AC自动机+可持久化数组优化)
- POJ - 2449 Remmarguts' Date(第k短路:spfa+A*)
- 0179-Largest Number( 最大数)
- mysql group by over,PostgreSQL相当于MySQL GROUP BY
- 【Boost】boost库asio详解9——TCP的简单例子2
- Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
- Docker常用操作命令(一)