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,96)">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,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,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,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 ifxmlIndentTreeOutput = 1 or xmlKeepBlanksDefault(0) was called

int xmlSaveFormatFileto ( 有格式整理的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)复制节点文件到内存

voidxmlDocDumpformatMemory (xmlDocPtrcur,
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)复制节点

xmlNodePtrxmlcopyNode (constxmlNodePtrnode,
int extended)

复制一个节点

node:

要复制的节点

extended:

0:仅仅添加节点名称,没有任何其他内容;1:添加节点所有内容包括子节点、属性等等,相当于有格式整理的xmlcopyNodeList;2:只添加节点本身内容和其自身属性

Returns:

一个新的节点指针,或者产生错误返回NULL;

xmlNodePtrxmlcopyNodeList (constxmlNodePtrnode)

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 (xmlChar*name)

Search and get thevalue of anattributeassociated to a node This does theentity substitution. This function looks in DTDattributedeclaration 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:

theattributename

Returns:

theattributevalue 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 firstxmlChar *

str2:

the secondxmlChar *

Returns:

1:相同,0:不同

int xmlStrcmp (const xmlChar * str2)

等同于strcmp

int xmlStrcasecmp (const xmlChar * str2)

等同于strcasecmp

int xmlStrncmp (const xmlChar * str2,int len)

等同于strncmp

int xmlStrncasecmp (const 等同于strncasecmp

int xmlUTF8Charcmp (const xmlChar * utf1,96)">xmlChar * utf2)

compares the twoUCS4 values

utf1:

pointer to first UTF8 char

utf2:

pointer to second UTF8 char

Returns:

result of the compare as withxmlStrncmp

int xmlStrQEqual (const xmlChar * pref,96)">xmlChar * name,96)">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 secondxmlChar *

Returns:

1 if they are equal,0 if they are different

参考文献:

http://xmlsoft.org/html/index.html

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念