什么是 XML ?
XML 指可擴展標記語言(eXtensible Markup Language)。
可擴展標記語言,標準通用標記語言的子集,一種用于標記電子文件使其具有結構性的標記語言。
它可以用來標記數據、定義數據類型,是一種允許用戶對自己的標記語言進行定義的源語言。 它非常適合萬維網傳輸,提供統一的方法來描述和交換獨立于應用程序或供應商的結構化數據。
XML解析器結構和API
XML的解析器主要有DOM和SAX兩種。
Ruby 中解析及創建 XML
RUBY中對XML的文檔的解析可以使用這個庫REXML庫。
REXML庫是ruby的一個XML工具包,是使用純Ruby語言編寫的,遵守XML1.0規范。
在Ruby1.8版本及其以后,RUBY標準庫中將包含REXML。
REXML庫的路徑是: rexml/document
所有的方法和類都被封裝到一個REXML模塊內。
REXML解析器比其他的解析器有以下優點:
以下為實例的 XML 代碼,保存為movies.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> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description></movie><movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description></movie></collection>
新聞熱點
疑難解答