大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 XML文件讀取的方法

XML文件讀取的方法

更新時間:2022-01-21 10:48:37 來源:動力節(jié)點(diǎn) 瀏覽1955次

本課將向您展示如何加載 XML 文件并訪問數(shù)據(jù)以在您的應(yīng)用程序中使用。XML 文件對于存儲首選項(xiàng)設(shè)置、使用 Web 以及需要與其他程序共享數(shù)據(jù)的情況非常有用。

LiveCode 提供了一個功能完善的庫來處理 XML 文件,這可能需要一點(diǎn)時間來適應(yīng),但使用起來非常簡單。

使用按鈕和字段創(chuàng)建堆棧

我們首先創(chuàng)建一個堆棧并將一個按鈕和一個字段拖到它上面。該按鈕將包含我們用于讀取 XML 文件的 LiveCode 代碼。該字段將包含從文件中讀取的結(jié)果數(shù)據(jù),將字段名稱設(shè)置為“信息”。將此堆棧保存在您的桌面上。

創(chuàng)建一個 XML 文件以用作示例

使用 Windows 上的記事本或 Mac OS X 上的 TextEdit 創(chuàng)建示例首選項(xiàng) XML 文件。該文件應(yīng)該是純文本。在桌面上將文件另存為“Preferences.xml”。XML 數(shù)據(jù)如下所示:

<preferences>
  <textColor>blue</textColor>
  <textSize>16</textSize>
  <introMessage size="18">Welcome guest, please click the button below to log in</introMessage>
  <recentDocuments>
    <recentDocument>C:/Documents and Settings/Administrator/Documents/DraftReport.txt</recentDocument>
    <recentDocument>C:/Reports/2009Final.txt</recentDocument>
    <recentDocument>C:/Documents and Settings/Administrator/Desktop/Temp.txt</recentDocument>
  </recentDocuments>
</preferences>

告訴按鈕在用戶單擊時加載文件

通過選擇它來編輯按鈕的腳本,然后單擊主菜單欄中的“腳本”按鈕。對于本例,我們將加載一個包含應(yīng)用程序首選項(xiàng)的 XML 文件。

使用以下代碼開始腳本:

1.點(diǎn)擊按鈕時,加載首選項(xiàng)并將它們放入字段中

on mouseUp
   loadPreferences
end mouseUp

2.加載首選項(xiàng)文件

加載首選項(xiàng)文件有兩個部分。第一部分是將文件讀入內(nèi)存并創(chuàng)建一個 XML“樹”。第二部分是處理樹并從中提取數(shù)據(jù)。

此函數(shù)讀取 XML 文件,并返回樹。樹表示為一個數(shù)字,實(shí)際的樹結(jié)構(gòu)和數(shù)據(jù)由 LiveCode 管理,因此我們無需擔(dān)心。

command loadPreferences
   local tTree
   put readPreferencesToXMLTree() into tTree
   if tTree is empty then
      exit loadPreferences
   end if 
   # Read the preferences we require from the tree and display them.
   processPreferencesTree tTree
 
   # Close the XML tree. 
   # This will free up the memory that the tree was using and prevent our application
   # using more memory than it needs or "leaking" memory by creating multiple trees
   # without closing any of them.
   revXMLDeleteTree tTree
end loadPreferences

請注意,這段代碼還沒有做任何事情,因?yàn)槲覀冞€沒有實(shí)現(xiàn)函數(shù) readPreferencesToXMLTree 和命令 processPreferencesTree。

從磁盤讀取 XML 文件

接下來,我們實(shí)現(xiàn)一個函數(shù)來讀取 XML。這分兩步完成,首先將文件讀入一個變量,就像任何其他文本文件一樣,其次,從文件中創(chuàng)建一個 XML“樹”。這棵樹允許我們輕松地操作 XML 數(shù)據(jù)。

添加代碼以讀取 XML 文件,如下所示。

private function readPreferencesToXMLTree
   # Find the XML file on disk. 
   # This is for now assumed to be in the same location as the stack / application.
   # Note that we restore the itemDelimiter to comma (its default value) afterwards.           	
   # This is not essential but its good practice to avoid tricky bugs 
   # that can arise due to unexpected delimiter values.
   set the itemDelimiter to slash
   local tPreferencesFile
   put item 1 to -2 of the effective filename of this stack & "/Preferences.xml" into tPreferencesFile
   set the itemDelimiter to comma 
   # Read the preferences data from the file into a variable. 
   # Always check for the result when reading files
   # as its possible that the file may have been deleted or moved.
   local tPreferencesData, tResult
   put url ("file:" & tPreferencesFile) into tPreferencesData
   put the result into tResult
   if tResult is not empty then
      answer error "Failed to read preferences file at location: " & tPreferencesFile
      return empty
   end if 
   # Create the XML "tree" from the data, 
   # checking to make sure that the file has loaded properly.
   # The revCreateXMLTree function will return a number 
   # (the tree's "handle" or "id") if it succeeds,
   # otherwise it will return a message saying why it failed.
   local tTree
   put revXMLCreateTree(tPreferencesData, false, true, false) into tTree
   if tTree is not an integer then
      answer error "Failed to process preferences file with error: " & tTree
      return empty
   end if 
   return tTree
end readPreferencesToXMLTree

從 XML 文件中提取信息并將其顯示在字段中

一旦我們有了 XML 樹,最后一步是使用 LiveCode 的 XML 庫從中獲取所需的信息。我們使用對 XML 庫的一系列調(diào)用來從樹中提取每條信息。

private command processPreferencesTree pTree
   # Extract the text color and text size preferences. 
   # These are simple nodes in the XML file,
   # we can get what is inside them using the revXMLNodeContents function
   # This function will return a string beginning with "xmlerr," 
   # if it fails, but we don't check this 
   # here as we created the file and we know it won't fail.
   local tTextColor
   put revXMLNodeContents(pTree, "preferences/textColor") into tTextColor 
   local tTextSize
   put revXMLNodeContents(pTree, "preferences/textSize") into tTextSize 
   # Extract the introductory message preference. 
   # This node has an attribute. We extract the contents and the 
   # attribute in two separate calls. 
   # The function revXMLAttribute allows us to read attributes from XML files,
   # its exactly the same as revXMLNodeContents, 
   # except that you also need to tell it which attribute you want.
   local tIntroMessage
   put revXMLNodeContents(pTree, "preferences/introMessage") into tIntroMessage 
   local tIntroMessageSize
   put revXMLAttribute(pTree, "preferences/introMessage", "size") into tIntroMessageSize 
   # Extract the recent documents list. 
   # This is a nested list of nodes, which could have any number of items.
   # First, we get a list of the recent documents, then we can loop 
   # through them and get each one in turn.
   # The revXMLChildNames function is useful for returning a list of nodes like this.  
   # The last parameter is important as it tells the function to return a unique
   # specifier for each node, allowing us to access them correctly. This will
   # look something like:
   #	recentDocument[1]
   #	recentDocument[2]
   #	recentDocument[3]
   local tRecentDocuments
   put revXMLChildNames(pTree, "preferences/recentDocuments", return, "recentDocument", true) into tRecentDocuments 
   # To get each document, we just use revXMLNodeContents again. 
   # However here we concatentate the name of each node
   # with the path that all recent document nodes have in common, 
   # to get the complete path.
   local tListOfRecentDocuments
   repeat for each line tRecentDocument in tRecentDocuments
      put revXMLNodeContents(pTree, "preferences/recentDocuments/" & tRecentDocument) & return after tListOfRecentDocuments
   end repeat
   delete the last char of tListOfRecentDocuments 
   # Now we output what we read from the file to see if it worked.
   local tOutput
   put "Text Color = " & tTextColor & return after tOutput
   put "Text Size = " & tTextSize & return after tOutput
   put return after tOutput
   put "Introductory Message (size: " & tIntroMessageSize & ") = " & return after tOutput
   put tIntroMessage & return & return after tOutput
   put "Recent Documents = " & return after tOutput
   put tListOfRecentDocuments after tOutput
   set the text of field "Information" to tOutput
end processPreferencesTree

測試

測試堆棧切換到運(yùn)行模式并單擊按鈕。

以上就是關(guān)于“XML文件讀取的方法”介紹,大家如果想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的Java視頻,里面的課程內(nèi)容詳細(xì),通俗易懂,適合小白學(xué)習(xí),希望對大家能夠有所幫助。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 欧美高清不卡午夜精品免费视频 | 99精品国产在现线免费 | 欧美成人一区二区三区不卡视频 | 4虎影院午夜在线观看 | 日韩高清一区二区三区不卡 | 国内自拍小视频 | 国产精品久久久久久久人热 | 久久91精品久久久久久水蜜桃 | 亚洲国产精品久久久天堂麻豆 | 亚洲最大的成人网 | 亚洲综合欧美日韩 | 天天干天天操天天 | 日日摸夜夜夜夜夜添 | 免费澳门一级毛片 | 日本成年一区久久综合 | 久草小区二区三区四区网页 | 国产成人精品午夜 | 日本特级aⅴ一级毛片 | 久草在线视频在线观看 | 米奇7777 | 国产人做人爱免费视频 | 亚洲精品国产成人一区二区 | 亚洲国内自拍愉拍20页 | 伊人热| 国产片一级aaa毛片视频 | 国产成人精品曰本亚洲77美色 | 抱着cao才爽视频 | 国产精品66| 美女在线看永久免费网址 | 日日夜夜亚洲 | 天天弄 | 久久91综合国产91久久精品 | 精品国产_亚洲人成在线高清 | 亚洲欧洲尹人香蕉综合 | 99久久香蕉国产综合影院 | 国产成人18黄禁网站免费观看 | 国产视频不卡 | 97影院3| 激情福利网 | 欧美亚洲高清 | 亚洲社区在线观看 |