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
總結
以上是生活随笔為你收集整理的libxml2如何解析xml格式的字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++之TR1::function
- 下一篇: map/multimap用法详解