Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML with Ruby

I'm way new to working with XML but just had a need dropped in my lap. I have been given an usual (to me) XML format. There are colons within the tags.

<THING1:things type="Container">   <PART1:Id type="Property">1234</PART1:Id>   <PART1:Name type="Property">The Name</PART1:Name> </THING1:things> 

It is a large file and there is much more to it than this but I hope this format will be familiar to someone. Does anyone know a way to approach an XML document of this sort?

I'd rather not just write a brute-force way of parsing the text but I can't seem to make any headway with REXML or Hpricot and I suspect it is due to these unusual tags.

my ruby code:

    require 'hpricot'     xml = File.open( "myfile.xml" )      doc = Hpricot::XML( xml )     (doc/:things).each do |thg|      [ 'Id', 'Name' ].each do |el|        puts "#{el}: #{thg.at(el).innerHTML}"      end    end 

...which is just lifted from: http://railstips.org/blog/archives/2006/12/09/parsing-xml-with-hpricot/

And I figured I would be able to figure some stuff out from here but this code returns nothing. It doens't error. It just returns.

like image 204
n8gard Avatar asked Jun 25 '12 22:06

n8gard


People also ask

How to Parse XML in Ruby?

Before we can process any XML or HTML documents in Ruby, we need to install the XML/HTML parser library. In this example, we shall use the Nokogiri library. Once installed, you can test it by launching the Ruby Interactive Shell with the IRB command.

What does Nokogiri do?

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 parser in asp net?

XML parser is a software library or a package that provides interface for client applications to work with XML documents. It checks for proper format of the XML document and may also validate the XML documents.


2 Answers

As @pguardiario mentioned, Nokogiri is the de facto XML and HTML parsing library. If you wanted to print out the Id and Name values in your example, here is how you would do it:

require 'nokogiri'  xml_str = <<EOF <THING1:things type="Container">   <PART1:Id type="Property">1234</PART1:Id>   <PART1:Name type="Property">The Name</PART1:Name> </THING1:things> EOF  doc = Nokogiri::XML(xml_str)  thing = doc.at_xpath('//things') puts "ID   = " + thing.at_xpath('//Id').content puts "Name = " + thing.at_xpath('//Name').content 

A few notes:

  • at_xpath is for matching one thing. If you know you have multiple items, you want to use xpath instead.
  • Depending on your document, namespaces can be problematic, so calling doc.remove_namespaces! can help (see this answer for a brief discussion).
  • You can use the css methods instead of xpath if you're more comfortable with those.
  • Definitely play around with this in irb or pry to investigate methods.

Resources

  • Parsing an HTML/XML document
  • Getting started with Nokogiri

Update

To handle multiple items, you need a root element, and you need to remove the // in the xpath query.

require 'nokogiri'  xml_str = <<EOF <root>   <THING1:things type="Container">     <PART1:Id type="Property">1234</PART1:Id>     <PART1:Name type="Property">The Name1</PART1:Name>   </THING1:things>   <THING2:things type="Container">     <PART2:Id type="Property">2234</PART2:Id>     <PART2:Name type="Property">The Name2</PART2:Name>   </THING2:things> </root> EOF  doc = Nokogiri::XML(xml_str) doc.xpath('//things').each do |thing|   puts "ID   = " + thing.at_xpath('Id').content   puts "Name = " + thing.at_xpath('Name').content end 

This will give you:

Id   = 1234 Name = The Name1  ID   = 2234 Name = The Name2 

If you are more familiar with CSS selectors, you can use this nearly identical bit of code:

doc.css('things').each do |thing|   puts "ID   = " + thing.at_css('Id').content   puts "Name = " + thing.at_css('Name').content end 
like image 115
jmdeldin Avatar answered Sep 20 '22 23:09

jmdeldin


If in a Rails environment, the Hash object is extended and one can take advantage of the the method from_xml:

xml = File.open("myfile.xml") data = Hash.from_xml(xml) 
like image 26
IliasT Avatar answered Sep 20 '22 23:09

IliasT