libxml2如何解析xml格式的字符串
1.?????xmlParseMemory,字符串轉(zhuǎn)為XML文檔
2.?????xmlDocGetRootElement,獲取XML文檔根節(jié)點
3.?????xmlStrcmp,比較XML字符串,與strcmp差不多
4.?????curr = curr->xmlChildrenNode,XML節(jié)點指針指向第一個子節(jié)點
5.?????curr = curr->next,XML節(jié)點指針指向下一個兄弟節(jié)點
6.?????xmlNodeGetContent,獲取XML節(jié)點的內(nèi)容
7.?????xmlFreeDoc,釋放節(jié)點,與free差不多
?
1.????????文件操作函數(shù)
a)????????保存文件
int???? xmlSaveFile??????????????????? (const char * filename, xmlDocPtr cur)將一個內(nèi)存中的文檔,保存到一個文件當中。如果編譯使用了壓縮功能,并且啟用了,這個函數(shù)會默認使用壓縮(壓縮也就是忽略文件當中的格式)。如果設(shè)定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)
將一個文本保存在文件當中,并且按照要求轉(zhuǎn)換到目標字符集,例如: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緩存當中。如果這個緩存已經(jīng)通過?xmlOutputBufferClose()?關(guān)閉掉了,那么將失敗。
| 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)
格式化的將內(nèi)存文檔保存到一個文件當中,格式設(shè)定與?xmlDocDumpFormatMemory()中一樣。
| filename: | the? filename (or URL) |
| cur: | the? document |
| format: | 一般都設(shè)定為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()。一般都采用這個函數(shù)進行內(nèi)存DOC指針的保存工作。
在調(diào)用這個函數(shù)以前,最好調(diào)用如下兩個語句,來整理內(nèi)容:
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: | 一般都設(shè)定為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: | 一般都設(shè)定為1 |
| Returns: | the? number of bytes written or -1 in case of failure. |
?
b)????????復制節(jié)點文件到內(nèi)存
void????xmlDocDumpFormatMemory???????? (xmlDocPtr?cur,?
????????????????????????????????????? ?xmlChar?**mem,?
???????????????????????????????????????int * size,?
????????????????????????????????????? ?int format)
將一個XML文檔指針復制到內(nèi)存當中,并且返回他的內(nèi)存字符指針和容量大小。返回的內(nèi)存需要顯性的調(diào)用xmlFree函數(shù)釋放內(nèi)存。注意,在xmlIndentTreeOutput = 1或者調(diào)用了xmlKeepBlanksDefault(0)函數(shù),@format = 1的設(shè)定才能起到作用。這個函數(shù)應該可以對輸出的文本進行一定的格式調(diào)整,而xmlDocDumpMemory函數(shù)不會進行調(diào)整,這就是兩者的區(qū)別。
通過這個函數(shù)建立的xmlChar,需要調(diào)用xmlFree進行內(nèi)存釋放。
| cur: | 文檔指針 |
| mem: | OUT:?內(nèi)存中的BUFF?指針 |
| size: | OUT: BUFF?長度 |
| format: | 一般都設(shè)定為1 |
在調(diào)用這個函數(shù)以前,最好調(diào)用如下兩個語句,來整理內(nèi)容:
xmlKeepBlanksDefault(0);
xmlIndentTreeOutput= 1;
c)????????從內(nèi)存中復制到一個文檔結(jié)構(gòu)中
xmlDocPtr????? xmlParseMemory???????? (const char * buffer, int size)通過內(nèi)存中的BUFF,建立一個DOC文檔。通過這個函數(shù)建立DOC文檔以后,這個文檔需要使用?xmlFreeDoc函數(shù)進行內(nèi)存釋放。
| buffer: | BUFF指針 |
| size: | BUFF中內(nèi)容的長度 |
| Returns: | 新建立的文檔指針 |
?
2.????????節(jié)點操作函數(shù)
a)????????復制節(jié)點
xmlNodePtr?????xmlCopyNode??????????? (constxmlNodePtrnode,?
????????????????????????????????????? ?int extended)
復制一個節(jié)點
| node: | 要復制的節(jié)點 |
| extended: | 0:僅僅添加節(jié)點名稱,沒有任何其他內(nèi)容;1:添加節(jié)點所有內(nèi)容,包括子節(jié)點、屬性等等,相當于有格式整理的xmlCopyNodeList;2:只添加節(jié)點本身內(nèi)容和其自身屬性; |
| Returns: | 一個新的節(jié)點指針,或者產(chǎn)生錯誤返回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
總結(jié)
以上是生活随笔為你收集整理的libxml2如何解析xml格式的字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++之TR1::function
- 下一篇: map/multimap用法详解