跳至內容

XML - 資料交換管理/使用 XML 編輯器

來自華夏公益教科書,開放的書籍,開放的世界

NetBeans

使用 XML 編輯器:NetBeans : 單實體

[編輯 | 編輯原始碼]

返回到單實體章節 => [[../A single entity/]

XML 文件

[編輯 | 編輯原始碼]
  1. 轉到 檔案 -> 新建檔案 -> XML -> XML 文件。
  2. 將檔案儲存為 'city'
  3. 選擇 XML 架構約束的文件作為文件型別。
  4. 接下來,指定您的架構位置(轉到瀏覽,然後選擇剛剛建立的架構)和根元素名稱(tourGuide)。
  5. 將上面的程式碼複製貼上到 NetBeans 編輯器中並儲存。
  6. 檢查 XML 文件以確保其格式正確(ALT-F9)。

要驗證 XML 文件

  1. 右鍵單擊文件的節點並選擇驗證 XML。

如果 XML 檔案 city.xml 符合提供的 XML 架構 city.xsd,您的輸出視窗應如下所示

Output Window - Validity check

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

Netbeans 提示

[編輯 | 編輯原始碼]

與元素型別 "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 樣式表文檔,都可以檢查以確保它們“格式良好”。如果 XML 文件符合 XML 語法規則,則稱其為格式良好的,如第 1 章所述,例如必須只有一個根元素,每個開始標籤都必須有一個結束標籤。可以使用 XML 編輯器軟體(在本例中為 NetBeans)檢查 XML 文件的格式良好性。如果檢測到任何錯誤,則會在輸出視窗中列出錯誤以及行號。此位置不一定是發生錯誤的行。雙擊任何錯誤訊息,您可以轉到檢測到錯誤的 XML 文件中的行。

使用 NetBeans 建立一個新專案

  1. 轉到 檔案 -> 新建專案

使用 NetBeans 建立上面的 XML 架構

  1. 轉到 檔案 -> 新建檔案 -> XML -> XML 架構。
  2. 將檔案儲存為 'city'(這會給檔案一個 .xsd 的副檔名)。
  3. 將上面的架構複製貼上到 city.xsd 中並儲存

要檢查 XML 文件以檢視其是否格式良好

  1. 右鍵單擊文件節點(在檔案下,city.xsd)並選擇檢查 XML 架構或按 Alt-F9。

如果 XML 架構 city.xsd 格式良好,您的輸出視窗應如下所示:Well Form XML Document

Summary: The XML schema shows the structure and elements of an XML document.

Netbeans 提示

[編輯 | 編輯原始碼]

元素型別 "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.

XML 樣式表

[編輯 | 編輯原始碼]

使用 NetBeans 建立上面的 XML 樣式表

  1. 轉到 檔案 -> 新建檔案 -> XML -> XSL 樣式表
  2. 將檔案儲存為 'city'
  3. 將上面的程式碼複製貼上到 NetBeans 編輯器中並儲存。
  4. 檢查樣式表以確保其格式良好(ALT-F9)。

注意:城市示例檔案(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 行,該行類似於 xmlns='http://xml.netbeans.org/examples/targetNS',否則它將不會顯示所有資料。


Netbeans 提示

[編輯 | 編輯原始碼]

元素型別 "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.
華夏公益教科書