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差不多
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. |
将一个文本保存在文件当中,并且按照要求转换到目标字符集,例如: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. |
将文件保存到一个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. |
格式化的将内存文档保存到一个文件当中,格式设定与xmlDocDumpFormatMemory()中一样。
filename: |
the filename (or URL) |
cur: |
the document |
format: |
一般都设定为1 |
Returns: |
the number of bytes written or -1 in case of failure. |
有格式整理的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 |
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)复制节点文件到内存
将一个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)从内存中复制到一个文档结构中
通过内存中的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.属性操作
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)字符串比较
判断两个字符串是否相同,比xmlStrcmp的速度要快一点。
str1: |
the firstxmlChar * |
str2: |
the secondxmlChar * |
Returns: |
1:相同,0:不同 |
等同于strcmp
等同于strcasecmp
等同于strncmp
compares the twoUCS4 values
utf1: |
pointer to first UTF8 char |
utf2: |
pointer to second UTF8 char |
Returns: |
result of the compare as withxmlStrncmp |
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
原文链接:https://www.f2er.com/xml/295087.html