
- Rexx 教程
- Rexx - 首頁
- Rexx - 概述
- Rexx - 環境
- Rexx - 安裝
- Rexx - 外掛安裝
- Rexx - 基本語法
- Rexx - 資料型別
- Rexx - 變數
- Rexx - 運算子
- Rexx - 陣列
- Rexx - 迴圈
- Rexx - 決策
- Rexx - 數字
- Rexx - 字串
- Rexx - 函式
- Rexx - 棧
- Rexx - 檔案I/O
- Rexx - 檔案函式
- Rexx - 子程式
- Rexx - 內建函式
- Rexx - 系統命令
- Rexx - XML
- Rexx - Regina
- Rexx - 解析
- Rexx - 訊號
- Rexx - 除錯
- Rexx - 錯誤處理
- Rexx - 面向物件
- Rexx - 可移植性
- Rexx - 擴充套件函式
- Rexx - 指令
- Rexx - 實現
- Rexx - Netrexx
- Rexx - Brexx
- Rexx - 資料庫
- 手持式和嵌入式
- Rexx - 效能
- Rexx - 最佳程式設計實踐
- Rexx - 圖形使用者介面
- Rexx - Reginald
- Rexx - Web程式設計
- Rexx 有用資源
- Rexx - 快速指南
- Rexx - 有用資源
- Rexx - 討論
Rexx - XML
XML 是一種可移植的開源語言,允許程式設計師開發可被其他應用程式讀取的應用程式,而不管作業系統和/或開發語言如何。這是用於在應用程式之間交換資料的最常用語言之一。
什麼是XML?
可擴充套件標記語言 XML 是一種標記語言,類似於 HTML 或 SGML。這是全球資訊網聯盟推薦的開放標準。XML 對於跟蹤少量到中等數量的資料非常有用,而無需基於 SQL 的骨幹。
對於我們所有的 XML 程式碼示例,讓我們使用以下簡單的 XML 檔案 **movies.xml** 來構建 XML 檔案並隨後讀取檔案。
<collection shelf = "New Arrivals"> <movie title = "Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description> </movie> <movie title = "Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description> </movie> <movie title = "Trigun"> <type>Anime, Action</type> <format>DVD</format> <year>1986</year> <rating>PG</rating> <stars>10</stars> <description>Vash the Stam pede!</description> </movie> <movie title = "Ishtar"> <type>Comedy</type> <format>VHS</format> <year>1987</year> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom </description> </movie> </collection>
入門
預設情況下,Rexx 直譯器中不包含 xml 功能。為了在 Rexx 中使用 XML,需要執行以下步驟。
下載以下檔案:
Rexxxml − www.interlog.com/~ptjm/
Libxml2 − www.ctindustries.net/libxml/
iconv-1.9.2.win32 − www.xmlsoft.org/sources/win32/oldreleases/
libxslt-1.1.26.win32 − www.xmlsoft.org/sources/win32/oldreleases/
解壓所有檔案並確保它們包含在系統路徑中。
載入 XML 函式
一旦下載併成功註冊了上一節中的所有檔案,下一步就是編寫程式碼來載入 Rexx XML 函式。這是用以下程式碼完成的。
rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs
關於上述程式,可以注意到以下幾點:
函式 **rxfuncadd** 用於載入外部庫。**xmlloadfuncs** 函式用於將 **rexxxml** 檔案中的所有庫載入到記憶體中。
如果 rcc<>0,則會導致錯誤。為此,我們可以呼叫 **rxfuncerrmsg** 來獲得有關錯誤訊息的更多詳細資訊。
我們最終呼叫 **xmlloadfuncs**,以便現在可以在 Rexx 程式中啟用所有與 xml 相關的功能。
讓我們看看 **Rexx 中可用的各種 XML 方法**。
xmlVersion
此方法返回系統上使用的 XML 和 XSLT 庫的版本。
語法
xmlVersion()
引數
無
返回值
此方法返回系統上使用的 XML 和 XSLT 庫的版本。
示例
rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion()
執行上述程式時,我們將得到以下結果。這再次取決於系統上使用的 XML 庫的版本。
輸出
1.0.0 20631 10126
xmlParseXML
此函式用於解析傳送到函式的 XML 資料。文件樹由函式返回。
語法
xmlParseXML(filename)
引數
**檔名** - 這是需要解析的 XML 檔案的名稱。
返回值
文件樹由函式返回。否則返回 0,如果出現錯誤。
示例
rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion() sw = xmlParseXML('test.xml')
輸出
無一般輸出。
xmlFindNode
此方法評估傳遞給它的 **XPath 表示式**。這用於解析文件樹以產生可以進一步處理的 **節點集**。
語法
xmlParseXML(XPath,document)
引數
**XPath** - 這是 xml 檔案中節點的路徑。
**文件** - 這是 XML 文件
返回值
評估 XPath 表示式並將結果作為稍後可用的節點集返回。
示例
rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion() document = xmlParseXML('test.xml') nodeset = xmlFindNode('//movie', document) say xmlNodesetCount(nodeset)
執行上述程式時,我們將得到以下結果。
輸出
4
輸出顯示了我們 xml 列表中的電影節點數
xmlEvalExpression
以下方法用於評估 XPath 表示式並返回字串作為結果。
語法
xmlParseXML(XPath,Node)
引數
**XPath** - 這是 xml 檔案中節點的路徑。
**文件** - 特定的節點元素。
返回值
根據傳送給它的 XPath 表示式返回一個字串。
示例
rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs document = xmlParseXML('test.xml') nodeset = xmlFindNode('//movie', document) do j = 1 to xmlNodesetCount(nodeset) value = xmlEvalExpression('type', xmlNodesetItem(nodeset, j)) say value end
執行上述程式時,我們將得到以下結果。
輸出
War, Thriller Anime, Science Fiction Anime, Action Comedy