2011-04-28 68 views
3

我解析使用引入nokogiri與下面的代碼片段的XML文件中查找元素:引入nokogiri和名稱

doc.xpath('//root').each do |root| 
    puts "# ROOT found" 
    root.xpath('//page').each do |page| 
    puts "## PAGE found/#{page['id']}/#{page['name']}/#{page['width']}/#{page['height']}" 
    page.children.each do |content| 
     ... 
    end 
    end 
end 

我如何通過在頁面元素的所有元素解析?有三個不同的元素:圖像,文字和視頻。我怎樣才能爲每個元素做一個案例陳述?

回答

10

老實說,你看起來很接近我..

doc.xpath('//root').each do |root| 
    puts "# ROOT found" 
    root.xpath('//page').each do |page| 
    puts "## PAGE found/#{page['id']}/#{page['name']}/#{page['width']}/#{page['height']}" 
    page.children.each do |child| 
     case child.name 
     when 'image' 
      do_image_stuff 
     when 'text' 
      do_text_stuff 
     when 'video' 
      do_video_stuff 
     end 
    end 
    end 
end 
+0

感謝。實際上使用匹配?(選擇器)方法自己解決它:) – Tronic 2011-04-29 06:24:23

5

兩個引入nokogiri的CSS和XPath存取允許指定多個標籤,這對於這類問題非常有用。而不是穿行在文檔的page標籤中的每個標籤:

require 'nokogiri' 

doc = Nokogiri::XML(' 
    <xml> 
    <body> 
    <image>image</image> 
    <text>text</text> 
    <video>video</video> 
    <other>other</other> 
    <image>image</image> 
    <text>text</text> 
    <video>video</video> 
    <other>other</other> 
    </body> 
    </xml>') 

這是一個搜索使用CSS:

doc.search('image, text, video').each do |node| 
    case node.name 
    when 'image' 
    puts node.text 
    when 'text' 
    puts node.text 
    when 'video' 
    puts node.text 
    else 
    puts 'should never get here' 
    end 
end 

# >> image 
# >> image 
# >> text 
# >> text 
# >> video 
# >> video 

注意它的CSS訪問指定它的順序返回標籤。如果你需要在文檔中標記的順序,可以使用XPath:

doc.search('//image | //text | //video').each do |node| 
    puts node.text 
end 

# >> image 
# >> text 
# >> video 
# >> image 
# >> text 
# >> video 

在這兩種情況下,因爲所有的搜索中的libxml發生時,只返回你需要爲Ruby的處理節點的程序應該運行得更快。

如果您需要將搜索限制一個<page>標籤內,你可以做一個搜索在鋒線上找到page節點,然後搜索它下面:

doc.at('page').search('image, text, video').each do |node| 
    ... 
end 

​​