Ruby解析XML文件

来源:互联网 发布:c语言循环移位指令 编辑:程序博客网 时间:2024/06/01 09:23

分类: 程序设计 928人阅读 评论(0) 收藏 举报
rubyxmlrubyminerecursionoutputencoding

Ruby是一款对开发版本依赖度很高的语言,而且目前不支持中文(如果要使用中文字符,可使用UTF-8)

 

以下程序在 Ruby 1.8.6 下编译通过, IDE环境为 RubyMine [比NetBean好用]

 

[ruby] view plaincopy
  1. # 解析XML文件  
  2.   
  3. require "rexml/document"  
  4. include REXML  
  5. puts "-- BEGIN --"  
  6. doc = Document.new(File.open("TestXML.xml"))  
  7. #doc.elements.each('BoardComposant') do |elem|  
  8. #  cr_ele = elem.get_elements('ComponantRectangle')  
  9. #  puts cr_ele  
  10. #end  
  11. doc.elements.each("BoardComposant/ComponantRectangle"){  |elem|  
  12.   if "second" == elem.attributes["name"]  then  
  13.     puts " => 2th"  
  14.     puts elem.elements[2].elements["Height"].text  
  15.     elem.elements.each{ |e|  
  16.       puts e.elements["Height"].text    
  17.     }  
  18.   end  
  19. }  
  20. #----------------------------------------------  
  21. puts "-----------------------------------------"  
  22. #----------------------------------------------  
  23. input = File.new("book.xml")  
  24. doc = Document.new(input)  
  25.   
  26. root = doc.root  
  27. puts root.attributes["shelf"]          # Recent Acquisitions   
  28.   
  29. doc.elements.each("library/section") { |e| puts e.attributes["name"] }  
  30. #  Output:   
  31. #    Ruby   
  32. #    Space   
  33.   
  34. doc.elements.each("*/section/book") { |e| puts e.attributes["isbn"] }  
  35. # Output:   
  36. #  0672328844   
  37. #  0321445619   
  38. #  0684835509   
  39. #  074325631X   
  40.   
  41. sec2 = root.elements[2]  
  42. author = sec2.elements[1].elements["author"].text         # Robert Zubrin  
  43. puts author  
  44. puts "-- End --"  

 

以下是待解析的XML文件

 

book.xml

[xhtml] view plaincopy
  1. <library shelf="Recent Acquisitions">   
  2. <section name="Ruby">   
  3. <book isbn="0672328844">   
  4. <title>The Ruby Way</title>   
  5. <author>Hal Fulton</author>   
  6. <description>   
  7. Second edition. The book you are now reading.   
  8. Ain't recursion grand?   
  9. </description>   
  10. </book>   
  11. </section>   
  12. <section name="Space">   
  13. <book isbn="0684835509">   
  14. <title>The Case for Mars</title>   
  15. <author>Robert Zubrin</author>   
  16. <description>Pushing toward a second home for the human   
  17. race.   
  18. </description>   
  19. </book>   
  20. <book isbn="074325631X">   
  21. <title>First Man: The Life of Neil A. Armstrong</title>   
  22. <author>James R. Hansen</author>   
  23. <description>Definitive biography of the first man on   
  24. the moon. </description>   
  25. </book>   
  26. </section>   
  27. </library>  

 

TestXML.xml

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <BoardComposant>  
  3.   <ComponantRectangle name="first" X="97" Y="186" Height="382" Width="587" >  
  4.     <JoinSpot>  
  5.       <Position>  
  6.         <X>147</X>  
  7.         <Y>219</Y>  
  8.       </Position>  
  9.       <Height>76</Height>  
  10.       <Width>86</Width>  
  11.     </JoinSpot>  
  12.     <JoinSpot>  
  13.       <Position>  
  14.         <X>510</X>  
  15.         <Y>236</Y>  
  16.       </Position>  
  17.       <Height>65</Height>  
  18.       <Width>77</Width>  
  19.     </JoinSpot>  
  20.     <JoinSpot>  
  21.       <Position>  
  22.         <X>194</X>  
  23.         <Y>380</Y>  
  24.       </Position>  
  25.       <Height>108</Height>  
  26.       <Width>83</Width>  
  27.     </JoinSpot>  
  28.     <JoinSpot>  
  29.       <Position>  
  30.         <X>417</X>  
  31.         <Y>400</Y>  
  32.       </Position>  
  33.       <Height>85</Height>  
  34.       <Width>167</Width>  
  35.     </JoinSpot>  
  36.   </ComponantRectangle>  
  37.     
  38.   <ComponantRectangle name="second" X="733" Y="179" Height="410" Width="343">  
  39.     <JoinSpot>  
  40.       <Position>  
  41.         <X>770</X>  
  42.         <Y>221</Y>  
  43.       </Position>  
  44.       <Height>78</Height>  
  45.       <Width>70</Width>  
  46.     </JoinSpot>  
  47.     <JoinSpot>  
  48.       <Position>  
  49.         <X>916</X>  
  50.         <Y>259</Y>  
  51.       </Position>  
  52.       <Height>68</Height>  
  53.       <Width>92</Width>  
  54.     </JoinSpot>  
  55.     <JoinSpot>  
  56.       <Position>  
  57.         <X>839</X>  
  58.         <Y>416</Y>  
  59.       </Position>  
  60.       <Height>109</Height>  
  61.       <Width>62</Width>  
  62.     </JoinSpot>  
  63.     <JoinSpot>  
  64.       <Position>  
  65.         <X>953</X>  
  66.         <Y>410</Y>  
  67.       </Position>  
  68.       <Height>85</Height>  
  69.       <Width>63</Width>  
  70.     </JoinSpot>  
  71.   </ComponantRectangle>  
  72.     
  73.   <ComponantRectangle name="third" X="126" Y="624" Height="282" Width="471">  
  74.     <JoinSpot>  
  75.       <Position>  
  76.         <X>179</X>  
  77.         <Y>658</Y>  
  78.       </Position>  
  79.       <Height>108</Height>  
  80.       <Width>133</Width>  
  81.     </JoinSpot>  
  82.   </ComponantRectangle>  
  83.     
  84.   <ComponantRectangle name="forth" X="333" Y="122" Height="282" Width="471" />  
  85. </BoardComposant>  

原创粉丝点击