Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Nokogiri to parse an XML file?

I'm having some issues with Nokogiri.

I am trying to parse this XML file:

<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>

I want all of that information displayed to the screen, that's it. Should be simple right? I am doing this:

require 'nokogiri'

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

Each Items is a node, and under that there are children nodes of Item?

I create a map of this, which returns a hash, and the code in {} goes through each node and places the children text into @block. Then I can display all of the child node's text to the screen.

I have no idea how far or close I am, because I've read so many articles, and am still a little confused on the basics especially since usually with a new language, I read from a file and output to the screen for a basic program.

like image 413
camdixon Avatar asked Jul 11 '13 17:07

camdixon


People also ask

Which gem is used to parse a .XML or .HTML document?

To parse XML-documents, I recommend the gem nokogiri .

What is the use of Nokogiri?

Nokogiri (htpp://nokogiri.org/) is the most popular open source Ruby gem for HTML and XML parsing. It parses HTML and XML documents into node sets and allows for searching with CSS3 and XPath selectors. It may also be used to construct new HTML and XML objects.

What does Rails use Nokogiri for?

Nokogiri (鋸) makes it easy and painless to work with XML and HTML from Ruby. It provides a sensible, easy-to-understand API for reading, writing, modifying, and querying documents. It is fast and standards-compliant by relying on native parsers like libxml2 (CRuby) and xerces (JRuby).

What is XML used for?

What is XML Used For? XML is one of the most widely-used formats for sharing structured information today: between programs, between people, between computers and people, both locally and across networks. If you are already familiar with HTML, you can see that XML is very similar.


1 Answers

The nodes in the sample XML are capitalized, so your code should reflect that. For example:

require 'nokogiri'

doc = Nokogiri::XML(File.open("sample.xml"))
@block = doc.css("Items Item").map { |node| node.children.text }
puts @block
like image 148
orde Avatar answered Oct 03 '22 23:10

orde