2013-07-11 4 views
10

Ich habe ein paar Probleme mit Nokogiri.Wie verwende ich Nokogiri zum Parsen einer XML-Datei?

Ich versuche, diese XML-Datei zu analysieren:

<Collection version="2.0" id="74j5hc4je3b9"> 
    <Name>A Funfair in Bangkok</Name> 
    <PermaLink>Funfair in Bangkok</PermaLink> 
    <PermaLinkIsName>True</PermaLinkIsName> 
    <Description>A small funfair near On Nut in Bangkok.</Description> 
    <Date>2009-08-03T00:00:00</Date> 
    <IsHidden>False</IsHidden> 
    <Items> 
    <Item filename="AGC_1998.jpg"> 
     <Title>Funfair in Bangkok</Title> 
     <Caption>A small funfair near On Nut in Bangkok.</Caption> 
     <Authors>Anthony Bouch</Authors> 
     <Copyright>Copyright © Anthony Bouch</Copyright> 
     <CreatedDate>2009-08-07T19:22:08</CreatedDate> 
     <Keywords> 
     <Keyword>Funfair</Keyword> 
     <Keyword>Bangkok</Keyword> 
     <Keyword>Thailand</Keyword> 
     </Keywords> 
     <ThumbnailSize width="133" height="200" /> 
     <PreviewSize width="532" height="800" /> 
     <OriginalSize width="2279" height="3425" /> 
    </Item> 
    <Item filename="AGC_1164.jpg" iscover="True"> 
     <Title>Bumper Cars at a Funfair in Bangkok</Title> 
     <Caption>Bumper cars at a small funfair near On Nut in Bangkok.</Caption> 
     <Authors>Anthony Bouch</Authors> 
     <Copyright>Copyright © Anthony Bouch</Copyright> 
     <CreatedDate>2009-08-03T22:08:24</CreatedDate> 
     <Keywords> 
     <Keyword>Bumper Cars</Keyword> 
     <Keyword>Funfair</Keyword> 
     <Keyword>Bangkok</Keyword> 
     <Keyword>Thailand</Keyword> 
     </Keywords> 
     <ThumbnailSize width="200" height="133" /> 
     <PreviewSize width="800" height="532" /> 
     <OriginalSize width="3725" height="2479" /> 
    </Item> 
    </Items> 
</Collection> 

ich all das auf dem Bildschirm angezeigten Informationen wollen, das ist es. Sollte einfach sein oder? Ich tue dies:

require 'nokogiri' 

doc = Nokogiri::XML(File.open("sample.xml")) 
@block = doc.css("items item").map {|node| node.children.text} 
puts @block 

Jeder Items ist ein Knoten, und unter, dass es Kinder Knoten Item sind?

Ich erstelle eine Karte von diesem, die einen Hash zurückgibt, und der Code in {} geht durch jeden Knoten und platziert den untergeordneten Text in @block. Dann kann ich den gesamten Text des Kindknotens auf dem Bildschirm anzeigen.

Ich habe keine Ahnung, wie weit oder nah ich bin, weil ich so viele Artikel gelesen habe, und bin immer noch ein wenig verwirrt auf den Grundlagen besonders da ich normalerweise mit einer neuen Sprache aus einer Datei lese und ausgabe an die Bildschirm für ein Basisprogramm.

+0

Wenn Sie eine bestimmte Frage haben, sagen Sie es mir. Ich werde dir antworten. –

+0

Ich habe eine andere Frage. http: // Stapelüberlauf.com/questions/17600037/using-nokogiri-to-parse-xml-datei Es geht darum, wie man den Knotenbaum durchläuft. – camdixon

+0

verknüpfte Frage zeigt auf diesen Beitrag. –

Antwort

26

Hier werde ich versuchen, Ihnen zu erklären, alle Fragen/Verwirrungen Sie haben:

require 'nokogiri' 

doc = Nokogiri::XML.parse <<-XML 
<Collection version="2.0" id="74j5hc4je3b9"> 
    <Name>A Funfair in Bangkok</Name> 
    <PermaLink>Funfair in Bangkok</PermaLink> 
    <PermaLinkIsName>True</PermaLinkIsName> 
    <Description>A small funfair near On Nut in Bangkok.</Description> 
    <Date>2009-08-03T00:00:00</Date> 
    <IsHidden>False</IsHidden> 
    <Items> 
    <Item filename="AGC_1998.jpg"> 
     <Title>Funfair in Bangkok</Title> 
     <Caption>A small funfair near On Nut in Bangkok.</Caption> 
     <Authors>Anthony Bouch</Authors> 
     <Copyright>Copyright © Anthony Bouch</Copyright> 
     <CreatedDate>2009-08-07T19:22:08</CreatedDate> 
     <Keywords> 
     <Keyword>Funfair</Keyword> 
     <Keyword>Bangkok</Keyword> 
     <Keyword>Thailand</Keyword> 
     </Keywords> 
     <ThumbnailSize width="133" height="200" /> 
     <PreviewSize width="532" height="800" /> 
     <OriginalSize width="2279" height="3425" /> 
    </Item> 
    <Item filename="AGC_1164.jpg" iscover="True"> 
     <Title>Bumper Cars at a Funfair in Bangkok</Title> 
     <Caption>Bumper cars at a small funfair near On Nut in Bangkok.</Caption> 
     <Authors>Anthony Bouch</Authors> 
     <Copyright>Copyright © Anthony Bouch</Copyright> 
     <CreatedDate>2009-08-03T22:08:24</CreatedDate> 
     <Keywords> 
     <Keyword>Bumper Cars</Keyword> 
     <Keyword>Funfair</Keyword> 
     <Keyword>Bangkok</Keyword> 
     <Keyword>Thailand</Keyword> 
     </Keywords> 
     <ThumbnailSize width="200" height="133" /> 
     <PreviewSize width="800" height="532" /> 
     <OriginalSize width="3725" height="2479" /> 
    </Item> 
    </Items> 
</Collection> 
XML 

Also von meinem Verständnis von Nokogiri, die jeweils ‚Item‘ ist ein Knoten, und unter, dass gibt es Kinderknoten von 'Item'?

Nein, jeder ArtikelNokogiri::XML::NodeSet sind. Und darunter gibt es 2 Kinderknoten von Items, die von Nokogiri::XML::Element Klassenobjekt sind. Sie können sie auch sagen, Nokogiri::XML::Node

doc.class # => Nokogiri::XML::Document 
@block = doc.xpath("//Items/Item") 
@block.class # => Nokogiri::XML::NodeSet 
@block.count # => 2 
@block.map { |node| node.name } 
# => ["Item", "Item"] 
@block.map { |node| node.class } 
# => [Nokogiri::XML::Element, Nokogiri::XML::Element] 
@block.map { |node| node.children.count } 
# => [19, 19] 
@block.map { |node| node.class.superclass } 
# => [Nokogiri::XML::Node, Nokogiri::XML::Node] 

Wir erstellen eine Karte von dieser, die einen Hash gibt glaube ich, und der Code in {} geht durch jeden Knoten und legt den Kindern Text in @block . Dann kann ich den gesamten Text dieses Kindknotens auf dem Bildschirm anzeigen.

Ich verstehe das nicht. Obwohl ich versuchte, unten zu erklären, was ist Node, und was ist NodeSet in Nokogiri. Denken Sie daran, Nodeset ist eine Sammlung von Knoten.

@chld_class = @block.map do |node| 
    node.children.class 
end 
@chld_class 
# => [Nokogiri::XML::NodeSet, Nokogiri::XML::NodeSet] 
@chld_name = @block.map do |node| 
    node.children.map { |n| [n.name,n.class] } 
end 
@chld_name 
# => [[["text", Nokogiri::XML::Text], 
#  ["Title", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Caption", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Authors", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Copyright", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["CreatedDate", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Keywords", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["ThumbnailSize", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["PreviewSize", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["OriginalSize", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text]], 
#  [["text", Nokogiri::XML::Text], 
#  ["Title", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Caption", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Authors", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Copyright", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["CreatedDate", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["Keywords", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["ThumbnailSize", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["PreviewSize", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text], 
#  ["OriginalSize", Nokogiri::XML::Element], 
#  ["text", Nokogiri::XML::Text]]] 

@chld_name = @block.map do |node| 
    node.children.map{|n| [n.name,n.text.strip] if n.elem? }.compact 
end.compact 
@chld_name 
# => [[["Title", "Funfair in Bangkok"], 
#  ["Caption", "A small funfair near On Nut in Bangkok."], 
#  ["Authors", "Anthony Bouch"], 
#  ["Copyright", "Copyright © Anthony Bouch"], 
#  ["CreatedDate", "2009-08-07T19:22:08"], 
#  ["Keywords", "Funfair\n  Bangkok\n  Thailand"], 
#  ["ThumbnailSize", ""], 
#  ["PreviewSize", ""], 
#  ["OriginalSize", ""]], 
#  [["Title", "Bumper Cars at a Funfair in Bangkok"], 
#  ["Caption", "Bumper cars at a small funfair near On Nut in Bangkok."], 
#  ["Authors", "Anthony Bouch"], 
#  ["Copyright", "Copyright © Anthony Bouch"], 
#  ["CreatedDate", "2009-08-03T22:08:24"], 
#  ["Keywords", 
#  "Bumper Cars\n  Funfair\n  Bangkok\n  Thailand"], 
#  ["ThumbnailSize", ""], 
#  ["PreviewSize", ""], 
#  ["OriginalSize", ""]]] 
+0

Große Antwort! (y) –

+0

@LuizDamimn Danke Mann! –

4

Knoten in der XML-Beispiel werden aktiviert, so dass Ihr Code widerspiegeln sollte. Zum Beispiel:

require 'nokogiri' 

doc = Nokogiri::XML(File.open("sample.xml")) 
@block = doc.css("Items Item").map { |node| node.children.text } 
puts @block 
Verwandte Themen