1. 程式人生 > >libxml2如何解析xml格式的字串

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;

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 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

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:

str2:

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:

Returns:

1  if they are equal, 0 if they are different

參考文獻:

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


相關推薦

使用dom4j解析xml格式字串,獲取標籤屬性和內容

本文解析XML格式字串引入的是: dom4j-1.6.1.jar具體程式碼如下: /**      * 解析xml字串,獲取各項屬性內容      * readXML方法描述:      *      * @author : sunyan      * @createTi

libxml2如何解析xml格式字串

1.     xmlParseMemory,字串轉為XML文件 2.     xmlDocGetRootElement,獲取XML文件根節點 3.     xmlStrcmp,比較XML字串,與strcmp差不多 4.     curr = curr->xml

dom4j解析xml檔案 或者xml格式字串

  主要記錄的是專案中遇到的解析xml報文 獲取指定資料的方法,怕以後會忘,記錄一下   導包:dom4j(版本根據需要,不會造成相容錯誤就可以)  主要思路:利用遞迴的形式,解析xml檔案,首先附錄遞迴的方法,因為要求是需要把抓取到的資料放到指定檔案,並且現在只是一個de

使用 dom4j 解析XML格式字串的時候,字串沒問題,輸出結果總是空,也不報錯

遇到這型別問題,多半是因為字串的格式不對。可以嘗試著在字串中的結束標籤前面加幾個空格。 使用程式碼解決: /** * 將 String 型別的 XML 加空格和回車 * @param string * @return */ private Str

iOS開發之解析XML格式數據

trac pri dsta service ext inset ech dna 們的 XML格式的數據是一種數據的傳輸格式。因為它方便編寫。結構清晰,所以深受程序猿的喜愛,非常多人都喜歡使用XML格式數據傳輸或者作

【hive】解析url格式字串

解析url格式字串 parse_url() parse_url(url_str,’xxx’):第一個引數是url格式字串,第二個引數為要解析出來的屬性 parse_url(‘http://facebook.com/path/p1.php?query=1‘, ‘HOST’)返回’facebook.com’

【hive】解析json格式字串

(1)解析json中的單個屬性  get_json_object(json_str,’$.xxx’/‘$[xxx]’)   get_json_object函式第一個引數填寫json物件變數(string),第二個引數使用$表示json變數標識,然後用 . 或 [] 讀取物件或陣列 示例: ta

java解析xml格式請求報文

import java.io.File; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import

JAVA之SAX解析XML格式資料--Jdom.jar

package com.yldyyn.test; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.Namespace; import org.

Go語言解析xml格式資料

開發工具:Goland。 xml檔案,解析與json檔案解析非常相似。只是用了不同的包。 比如一個xml檔案,內容如下 <?xml version="1.0" encoding="utf-8"?> <servers version="1"> <

go使用xpath解析 xml 格式資訊

一:使用github.com/beevik/etree 包解析xml檔案、http請求返回值。 1.讀取檔案 doc := etree.NewDocument() if err := doc.ReadFromFile("bookstore.xml"); err != nil {

Tinyxml解析XML格式的檔案

 一,理論補充 1,xml是一種資料格式 2,本文的xml文件放在了D盤下面 3,Tinyxml解析庫包括2個.h檔案,4個.cpp檔案 4,引用Tinyxml庫檔案用“” 5,本文是讀取xml資

解析xml檔案-字串分組演算法-list分頁

import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; // 找到節點修改節點內的值.elements(“節點名稱”) SAXReader reade

Qt讀寫xml格式字串

Xml 簡介 W3CXML教程 XML是ExtensibleMarkup Language的縮寫,即可擴充套件標記語言。它是一種用來建立的標記的標記語言。使用XML標記語言可以做到資料或資料結構在任何程式語言環境下的共享。 XML 被設計用來傳輸和

通過JDK的JAXBContext類來轉換java實體類bean和xml格式字串的一些問題和解決辦法

    這兩天工作中用到web service來推送資料,涉及到xml的和java實體類的轉換,總結一下理解和碰到的問題。    (一)、寶寶想得到下面這樣的兩個xml型別的字串anth和args,來推送給第三方:<?xml version="1.0" encoding

解析xml格式的word文件(非jar包)-附程式碼

1 背景 dom4j,Jdom,w3c dom解析xml文件時速度比較慢,因此選擇自己寫程式碼解析xml文件。 2 思路 首先,對xml檔案進行分析,熟悉標籤; 然後,切分文件為不同的塊。我們要對文件中的書籤進行替換,因此需要將文件切分為書籤塊和非書籤塊; 還有就是

C# 解析json格式字串

 [Serializable] [DataContract] public class UserJson { [DataMember] public Int64 uid { get; set; }

Linux下使用libxml2解析XML配置檔案

這是一個專案過程中所遇到的問題。有多個商品種類,每個商品種類有著不同的基本資訊,現在我們需要對這些基本資訊進行驗證。但是每個種類會有自己獨特的驗證需求(不同種類需要驗證的欄位可能不一樣),如果我們使用程式碼來判斷每個種類需要驗證哪些欄位會非常麻煩,而且需求稍一變動就要修改

js怎麼解析json格式字串

json是種常用資料傳輸格式,js怎麼解析json格式的資料呢,請參閱下面的程式碼: <html> <head> <script type="text/javascript"> var data = '{"return

iOS 解析JSON格式字串

id json = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] optio