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)

有格式整理的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;

xmlNodePtr     xmlCopyNodeList        (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.

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.

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值