您的位置:首页 > 其它

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)        保存文件

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: