JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Languages on JVM » JRuby  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 REXML - ruby 的 xml 處理程式庫
cpi





發文: 12
積分: 0
於 2006-03-14 00:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請參考 http://anw.stikipad.com/ocean/show/REXML
REXML 是處理 xml 的程式庫, 可以讀入 xml 檔案然後建立 DOM tree, 而且完整支援 XPath, 所以比較好用. 若要輸出 xml 還是建議用 Xml Builder 比較快速.

1. 範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
      require 'rexml/document'
 
      f = File.new("inv.xml")
      doc = REXML::Document.new(f)
 
      doc.elements.each("inventory/section") { |element| 
        puts element.attributes["name"]
      }
 
      puts
      doc.elements.each("*/section/item") { |element| 
        puts element.elements["name"].text
        puts element.attributes["upc"] 
      }

2. inv.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
            <?xml version="1.0" encoding="UTF-8"?>
            <inventory title="OmniCorp Store #45x10^3">
              <section name="health">
                <item upc="123456789" stock="12">
                  <name>Invisibility Cream</name>
                  <price>14.50</price>
                  <description>Makes you invisible</description>
                </item>
                <item upc="445322344" stock="18">
                  <name>Levitation Salve</name>
                  <price>23.99</price>
                  <description>Levitate yourself for up to 3 hours per application</description>
                </item>
              </section>
              <section name="food">
                <item upc="485672034" stock="653">
                  <name>Blork and Freen Instameal</name>
                  <price>4.95</price>
                  <description>A tasty meal in a tablet; just add water</description>
                </item>
                <item upc="132957764" stock="44">
                  <name>Grob winglets</name>
                  <price>3.56</price>
                  <description>Tender winglets of Grob. Just add water</description>
                </item>
              </section>
            </inventory>


reply to postreply to post
作者 Re:REXML - ruby 的 xml 處理程式庫 [Re:cpi]
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2006-03-14 22:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Your example is part of Rexml tutorial.
For people who want to know more detail information about Rexml library, here are some links:

REXML Home page:
http://www.germane-software.com/software/rexml/

REXML Tutorial:
http://www.germane-software.com/software/rexml/docs/tutorial.html

BTW: Base on REXML Tutorial, I think that I found a bug:
Section: Using to_a()
all_upc_strings = doc.elements.to_a( "//item/attribute::upc" )

will just return empty array. The to_a works for element node but seems has trouble with attributes.


reply to postreply to post
» JWorld@TW »  Languages on JVM » JRuby

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8