libxml2如何解析xml格式的字符串
2016-06-21 10:29
357 查看
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) 保存文件
将一个内存中的文档,保存到一个文件当中。如果编译使用了压缩功能,并且启用了,这个函数会默认使用压缩(压缩也就是忽略文件当中的格式)。如果设定filanem为”-“,那么将会直接输出到 stdout。
将一个文本保存在文件当中,并且按照要求转换到目标字符集,例如:GB2312
将文件保存到一个I/O缓存当中。如果这个缓存已经通过 xmlOutputBufferClose() 关闭掉了,那么将失败。
格式化的将内存文档保存到一个文件当中,格式设定与 xmlDocDumpFormatMemory()中一样。
有格式整理的xmlSaveFileEnc()。一般都采用这个函数进行内存DOC指针的保存工作。
在调用这个函数以前,最好调用如下两个语句,来整理内容:
xmlKeepBlanksDefault(0);
xmlIndentTreeOutput= 1;
有格式整理的xmlSaveFileTo()。
b) 复制节点文件到内存
void xmlDocDumpFormatMemory (xmlDocPtr cur,
xmlChar **mem,
int * size,
int format)
将一个XML文档指针复制到内存当中,并且返回他的内存字符指针和容量大小。返回的内存需要显性的调用xmlFree函数释放内存。注意,在xmlIndentTreeOutput = 1或者调用了xmlKeepBlanksDefault(0)函数,@format = 1的设定才能起到作用。这个函数应该可以对输出的文本进行一定的格式调整,而xmlDocDumpMemory函数不会进行调整,这就是两者的区别。
通过这个函数建立的xmlChar,需要调用xmlFree进行内存释放。
在调用这个函数以前,最好调用如下两个语句,来整理内容:
xmlKeepBlanksDefault(0);
xmlIndentTreeOutput= 1;
c) 从内存中复制到一个文档结构中
通过内存中的BUFF,建立一个DOC文档。通过这个函数建立DOC文档以后,这个文档需要使用 xmlFreeDoc函数进行内存释放。
2. 节点操作函数
a) 复制节点
xmlNodePtr xmlCopyNode
(constxmlNodePtrnode,
int extended)
复制一个节点
xmlNodePtr xmlCopyNodeList
(const xmlNodePtr node)
Do a recursivecopy of the node list. Use xmlDocCopyNodeList() if possible to ensure stringinterning.
3. 属性操作
xmlChar * xmlGetProp
(xmlNodePtr node,
const xmlChar *name)
Search and get thevalue of an attribute associated to a node This does theentity substitution.
This function looks in DTD attribute declaration for #FIXED or defaultdeclaration values
unless DTD use has been turned off. NOTE: this function actsindependently of namespaces associated to the attribute. Use xmlGetNsProp() orxmlGetNoNsProp() for namespace aware processing.
4. 字符串操作
a) 字符串比较
判断两个字符串是否相同,比xmlStrcmp的速度要快一点。
等同于strcmp
等同于strcasecmp
等同于strncmp
等同于strncasecmp
compares the twoUCS4 values
Check if a QNameis Equal to a given string
参考文献:
▫ http://xmlsoft.org/html/index.html
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)
有格式整理的xmlSaveFileTo()。
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. |
b) 复制节点文件到内存
void xmlDocDumpFormatMemory (xmlDocPtr cur,
xmlChar **mem,
int * size,
int format)
将一个XML文档指针复制到内存当中,并且返回他的内存字符指针和容量大小。返回的内存需要显性的调用xmlFree函数释放内存。注意,在xmlIndentTreeOutput = 1或者调用了xmlKeepBlanksDefault(0)函数,@format = 1的设定才能起到作用。这个函数应该可以对输出的文本进行一定的格式调整,而xmlDocDumpMemory函数不会进行调整,这就是两者的区别。
通过这个函数建立的xmlChar,需要调用xmlFree进行内存释放。
cur: | 文档指针 |
mem: | OUT: 内存中的BUFF 指针 |
size: | OUT: BUFF 长度 |
format: | 一般都设定为1 |
xmlKeepBlanksDefault(0);
xmlIndentTreeOutput= 1;
c) 从内存中复制到一个文档结构中
xmlDocPtr xmlParseMemory (const char * buffer, int size)
通过内存中的BUFF,建立一个DOC文档。通过这个函数建立DOC文档以后,这个文档需要使用 xmlFreeDoc函数进行内存释放。
buffer: | BUFF指针 |
size: | BUFF中内容的长度 |
Returns: | 新建立的文档指针 |
2. 节点操作函数
a) 复制节点
xmlNodePtr xmlCopyNode
(constxmlNodePtrnode,
int extended)
复制一个节点
node: | 要复制的节点 |
extended: | 0:仅仅添加节点名称,没有任何其他内容;1:添加节点所有内容,包括子节点、属性等等,相当于有格式整理的xmlCopyNodeList;2:只添加节点本身内容和其自身属性; |
Returns: | 一个新的节点指针,或者产生错误返回NULL; |
(const xmlNodePtr node)
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. |
xmlChar * xmlGetProp
(xmlNodePtr node,
const xmlChar *name)
Search and get thevalue of an attribute associated to a node This does theentity substitution.
This function looks in DTD attribute declaration for #FIXED or defaultdeclaration values
unless DTD use has been turned off. NOTE: this function actsindependently of namespaces associated to the attribute. Use xmlGetNsProp() orxmlGetNoNsProp() for namespace aware processing.
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(). |
4. 字符串操作
a) 字符串比较
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)
等同于strcmp
int xmlStrcasecmp (const xmlChar * str1, const xmlChar * str2)
等同于strcasecmp
int xmlStrncmp (const xmlChar * str1, const xmlChar * str2, int len)
等同于strncmp
int xmlStrncasecmp (const xmlChar * str1, const xmlChar * str2, int len)
等同于strncasecmp
int xmlUTF8Charcmp (const xmlChar * utf1, const xmlChar * utf2)
compares the twoUCS4 values
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
相关文章推荐
- PHP输入流php://input介绍
- 这个是oschina的用法
- 欢迎使用CSDN-markdown编辑器
- jQuery 取值、赋值的基本方法整理
- JBoss Access log的配置
- iOS 设置UITextView显示文本的光标
- shell脚本 8种字符串截取的方法
- ubuntu更换阿里源
- makefile的语法及写法
- Spring MVC文件上传下载工具类
- [Informix] unload load
- IOS音频和视频(Audio & Video)
- 3款国外最好的免费项目管理软件
- 按钮的创建
- linux之cut用法
- Java开发中的23种设计模式详解
- python大规模数据处理技巧之二:机器学习中常用操作
- fileno函数
- struct结构体中的点
- C++ - PAT - 1040. 有几个PAT(25)