我對Nokogiri和Ruby頗爲陌生,希望得到一些幫助。如何在Nokogiri中使用SAX遍歷內部節點?
我正在使用class MyDoc < Nokogiri::XML::SAX::Document
解析一個非常大的XML文件。現在我想遍歷塊的內部。
這是我的XML文件的格式:
<Content id="83087">
<Title></Title>
<PublisherEntity id="1067">eBooksLib</PublisherEntity>
<Publisher>eBooksLib</Publisher>
......
</Content>
我已經可以告訴我們,如果「內容」標籤中找到,現在我想知道如何在其內部穿過。這裏是我縮短的代碼:
class MyDoc < Nokogiri::XML::SAX::Document
#check the start element. set flag for each element
def start_element name, attrs = []
if(name == 'Content')
#get the <Title>
#get the <PublisherEntity>
#get the Publisher
end
end
def cdata_block(string)
characters(string)
end
def characters(str)
puts str
end
end
爲什麼選擇SAX?您很有可能會在尋找Nokogiri.XML。 – pguardiario
xml文件至少是1GIG,所以我希望它具有內存高效性。 – Diffy
+1是的,這實際上是使用SAX的一個很好的理由 – pguardiario