XML - 資料交換管理/NetBeans
NetBeans
- 啟動 NetBeans
- 透過開啟幫助 > 幫助內容並閱讀入門中的資料來熟悉 IDE
- 透過檔案 > 新建專案..(Ctrl+Shift+N) 建立一個新專案
- 在“選擇一個專案”下,在類別中選擇:通用
- 在專案中選擇:Java 應用程式
- 按下一步
- 適當地命名專案名稱(例如,xml1)
- 選中“設定為主專案”並取消選中“建立主類”
- 儲存到適當的位置
- 按完成
- 從專案視窗中右鍵單擊剛剛建立的專案(例如,xml1)
- 透過新建 > 檔案/資料夾 > XML > XML 文件建立新檔案
- 命名檔案(例如,city)
- 按下一步
- 選擇“格式良好的文件”
- 按完成
- 您應該看到以下 XML 檔案框架
<?xml version="1.0" encoding="UTF-8"?> <!-- Document : city.xml Created on : December 26, 2005, 11:18 AM Author : rtw Description: Purpose of the document follows. --> <root> </root>
- 目標是建立一個簡單的標記檔案,而不是 XML 檔案,因此用表 1 中的程式碼行替換框架 XML 檔案中由
<root>和</root>分隔的部分。更改後,檔案應如下所示<?xml version="1.0" encoding="UTF-8"?> <!-- Document : city.xml Created on : December 26, 2005, 11:18 AM Author : rtw Description: Purpose of the document follows. --> <city> <cityname>Athens</cityname> <state>GA</state> <description> Home of the University of Georgia</description> <population>100,000</population> <location>Located about 60 miles Northeast of Atlanta</location> <latitude>33 57' 39"N</latitude> <longitude>83 22' 42" W</longitude> </city>
- 透過單擊文件列表上方 XML 工具欄上的單個綠色向下箭頭來檢查標記檔案是否格式良好。它應該透過檢查。或者,使用 Alt+F9(Windows)或 Ctrl+F9(Mac)。
- 刪除標籤
</city>並再次檢查檔案。這次您應該收到一個錯誤,指示缺少結束標籤。“XML 文件結構必須在同一個實體內開始和結束。”,後面是括號中的行號(錯誤發生的位置)應該以藍色顯示並帶下劃線。
NetBeans 是一款開源 IDE,它包含一個 XML 編輯器。
- 轉到檔案 -> 新建檔案 -> XML -> XML 文件。
- 將您的檔案儲存為“city”
- 選擇 XML 模式約束文件作為文件型別。
- 接下來,指定您的模式位置(轉到瀏覽,然後選擇剛剛建立的模式)和根元素名稱(tourGuide)。
- 將上面的程式碼複製貼上到 NetBeans 編輯器中並儲存。
- 檢查 XML 文件以確保它格式良好 (ALT-F9)。
要驗證 XML 文件
- 右鍵單擊文件的節點並選擇驗證 XML。
如果 XML 檔案 city.xml 符合提供的 XML 模式 city.xsd,您的輸出視窗應該如下所示
在 NetBeans 中開啟此檔案 city.xml。
Summary: Being a file which contains XML code and syntax, the XML document is an entity of neatly organized elements and subelements. |
與元素型別“Listing”關聯的屬性“xsi:noNamespaceSchemaLocation”的字首“xsi”未繫結。
-Caused because line
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" is
missing from the top of the document.
cvc-elt.1:找不到元素“Listing”的宣告。
-Caused due to lack of extension(.xsd) at the end of the schema
name.
cvc-complex-type.2.4.a:從元素“MuseumName”開始找到無效內容。預期為“{“”:Museum}”之一。[5]
-Caused due to lack of naming at the beginning and end of the
variable naming section.
元素型別“MuseumName”必須以匹配的結束標籤“</MuseumName>”終止。
-Could be caused by a missing "/" in the end tag of one of the
variables
任何 XML 文件,包括 XML 模式和 XML 樣式表文檔,都可以檢查以確保它們是“格式良好的”。如果 XML 文件符合第 1 章中概述的 XML 語法規則,則稱其為格式良好的,例如,必須恰好有一個根元素,每個開始標籤都必須有一個結束標籤。可以使用 XML 編輯器軟體(在本例中為 NetBeans)檢查 XML 文件的格式良好性。如果檢測到任何錯誤,則會在輸出視窗中列出錯誤以及行號。此位置不一定是錯誤發生的程式碼行。雙擊任何錯誤訊息,可以轉到檢測到錯誤的 XML 文件中的程式碼行。
使用 NetBeans 建立一個新專案
- 轉到檔案 -> 新建專案
使用 NetBeans 建立上面的 XML 模式
- 轉到檔案 -> 新建檔案 -> XML -> XML 模式。
- 將您的檔案儲存為“city”(這會為檔案賦予副檔名 .xsd)。
- 將上面的模式複製貼上到 city.xsd 中並儲存
要檢查 XML 文件以檢視它是否格式良好
- 右鍵單擊文件的節點(在檔案下,city.xsd)並選擇檢查 XML 模式或按 Alt-F9。
如果 XML 模式 city.xsd 格式良好,您的輸出視窗應該如下所示:
Summary: The XML schema shows the structure and elements of an XML document. |
元素型別“xsd:element”必須後跟屬性規範、“>”或“/>”。
-Caused by lack of ">" or "/>" symbol at the end of either the
specified line or the line there after.
與元素型別“xsd:complexType”關聯的屬性“name”的值不能包含“<”字元。
-Often caused by lack of closing quotation around the variable
name on either the specified line or the line there after.
src-resolve.4.2:解析元件“xsd:strin”時出錯…
-Caused by invalid "type", most likely misspelled. Can be for
any "type", not just "string."
元素型別“xsd:sequence”必須以匹配的結束標籤“</xsd:sequence>”終止。
-Caused because </xsd:sequence> is not found in order to close
the statement.
-Also caused because the closing statements are out of order as
they at the beginning of the Schema.
使用 NetBeans 建立上面的 XML 樣式表
- 轉到檔案 -> 新建檔案 -> XML -> XSL 樣式表
- 將您的檔案儲存為“city”
- 將上面的程式碼複製貼上到 NetBeans 編輯器中並儲存。
- 檢查樣式表以確保它格式良好 (ALT-F9)。
注意:city 示例檔案(city.xsd、city.xml 和 city.xsl)可以使用文字編輯器建立,結果在相容的 Web 瀏覽器(例如 Mozilla 1.6)中正確顯示。您需要將所有檔案儲存在同一個目錄中,並在 city.xml 的第 1 行之後插入以下程式碼行:<?xml-stylesheet href="city.xsl" type="text/xsl"?>
這將 XSL 樣式表與包含城市資料的 XML 檔案關聯起來。此外,如果您使用的是 Netbeans,請務必刪除開頭類似於 xmlns='http://xml.netbeans.org/examples/targetNS' 的 xmlns 行,否則它不會顯示所有資料。
元素型別“xsl:text”必須以匹配的結束標籤“</xsl:text>”終止。
-Caused by either lack of end-tag or misspelling of the end-
tag.
元素型別“xsl:for-each”必須以匹配的結束標籤“</xsl:for-each>”終止。
-Caused due to either lack of end-tag for the specified tag or
end-tags placed in the wrong order.