Skip to content
i5m edited this page Sep 13, 2010 · 1 revision

(Part of An Hpricot Showcase.)

The Hpricot.XML Method

The primary way to use Hpricot’s XML mode is to call the Hpricot.XML method:

 doc = open("http://redhanded.hobix.com/index.xml") do |f|
   Hpricot.XML(f)
 end

Or:

 doc = Hpricot.XML(open("http://redhanded.hobix.com/index.xml"))

Also, :fixup_tags is canceled out by the :xml option. This is because
:fixup_tags makes assumptions based how HTML is structured. Specifically, how
tags are defined in the XHTML 1.0 DTD.

The inner_html method can still be used on nodes even though you’re not working with HTML.

 (doc/:item).each do |item|
   title = (item/:title).inner_html
   link = (item/:link).inner_html
   date = (item/'dc:date').inner_html
   # ...
 end

Transforming XML

Hpricot provides the ability to transform a document. Most of the functionality is geared towards HTML, but can be used for XML as well.

[13:58:19] doki_pen@lemmy ~/src/hpricot $ cat hpricot_test.rb 
#!/usr/bin/env ruby
require 'hpricot'

xml = <<XML
<entries>
  <entry>
    <name>c</name>
    <value>d</value>
  </entry>
</entries>
XML

Hpricot::Builder.set :indent, 2

doc = Hpricot::XML(xml)

doc.at('entry').before do
  tag!'entry' do
    tag!'name', 'a'
    tag!'value', 'b'
  end
end

puts doc

[13:58:38] doki_pen@lemmy ~/src/hpricot $ ./hpricot_test.rb 
<entries>
  <entry><name>a</name><value>b</value></entry><entry>
    <name>c</name>
    <value>d</value>
  </entry>
</entries>
[13:58:41] doki_pen@lemmy ~/src/hpricot $ 

:indent doesn’t seem to help, how do we get it go format properly?

Return to An Hpricot Showcase.