# Process xml in a page

**URL:** <https://www.ruby-forum.com/t/process-xml-in-a-page/98777>\
**Category:** Ruby\
**Created:** [July 18, 2006, 3:30am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777 "2006-07-18T03:30:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [July 18, 2006, 3:30am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777/1 "2006-07-18T03:30:53Z")

</div>

Hi all,

I would like to process the xml in the url below, but I am unsure how.  
I’ve always had APIs to help me out before, so this is new.

[http://clinicaltrials.gov/show/NCT00001372?displayxml=true](http://clinicaltrials.gov/show/NCT00001372?displayxml=true)

I want to get this xml in an object that I can then parse and extract  
the portions I need.

Thank you for your help, as always.

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [July 18, 2006, 4:42am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777/2 "2006-07-18T04:42:41Z")

</div>

Hunter W. wrote:

> Thank you for your help, as always.  
> Use open-uri to fetch the XML, then load it into a REXML::Document  
> object.

Then use REXML’s XPath to grab what you need.

[http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/)  
[http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html)

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [July 19, 2006, 12:56am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777/3 "2006-07-19T00:56:40Z")

</div>

Thank you, James. I got a step further, but still need help.

This code outputs exactly what I want to see to the debug window:

require ‘open-uri’  
require “rexml/document”  
include REXML

url = “[http://clinicaltrials.gov/show/NCT00001372?displayxml=true](http://clinicaltrials.gov/show/NCT00001372?displayxml=true)”

open(url) { |page| print page.read() }

However, I am having trouble when using the REXML tutorial. Should I  
somehow save the output to an .xml file locally? Or should I add the  
output to a string? I am not sure how to do either as I am having  
trouble directing the “print” output anywhere.

Thank you and sorry for the noobness. The api I used in a ruby project  
before made everything a bit too easy, so I am still learning.

-Hunter

James B. wrote:

> Hunter W. wrote:
> 
> > Thank you for your help, as always.  
> > Use open-uri to fetch the XML, then load it into a REXML::Document  
> > object.
> 
> Then use REXML’s XPath to grab what you need.
> 
> [http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/](http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/)  
> [http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html)
> 
> –  
> James B.
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
> [Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
> [http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [July 19, 2006, 1:06am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777/4 "2006-07-19T01:06:02Z")

</div>

Hunter W. wrote:

> open(url) { |page| print page.read() }

require ‘open-uri’  
require “rexml/document”  
include REXML

url = “[http://clinicaltrials.gov/show/NCT00001372?displayxml=true](http://clinicaltrials.gov/show/NCT00001372?displayxml=true)”

xml = open(url).read  
p xml

doc = REXML::Document.new(xml)

p doc.root.name

–  
James B.

“I never dispute another person’s delusions, just their facts.”

- Len Bullard

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [July 19, 2006, 1:42am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777/5 "2006-07-19T01:42:07Z")

</div>

A guy in the comments on the page below had the same problem. Someone  
supplied a fix for windows (2nd link). It’as all good after that.

[http://redhanded.hobix.com/inspect/noXpathOnMessyHtmlIsJustAsEasyInRuby.html](http://redhanded.hobix.com/inspect/noXpathOnMessyHtmlIsJustAsEasyInRuby.html)

[http://www.dave.burt.id.au/ruby/iconv.zip](http://www.dave.burt.id.au/ruby/iconv.zip)

Thanks again, James!

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [July 19, 2006, 1:20am UTC](https://www.ruby-forum.com/t/process-xml-in-a-page/98777/6 "2006-07-19T01:20:40Z")

</div>

I get the error below after a successful XML output to the debug window.  
Thanks!

No such file to load – rexml/encodings/ASCII.rb  
No decoder found for encoding ASCII. Please install iconv.  
c:/ruby/lib/ruby/1.8/rexml/encoding.rb:33:in `encoding=' c:/ruby/lib/ruby/1.8/rexml/source.rb:40:in`encoding=’  
c:/ruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:202:in `pull' c:/ruby/lib/ruby/1.8/rexml/parsers/treeparser.rb:21:in`parse’  
c:/ruby/lib/ruby/1.8/rexml/document.rb:176:in `build' c:/ruby/lib/ruby/1.8/rexml/document.rb:45:in`initialize’  
C:/Documents and Settings/hwalker/Desktop/Ruby-1.rb:13:in `new’  
C:/Documents and Settings/hwalker/Desktop/Ruby-1.rb:13

c:/ruby/lib/ruby/1.8/rexml/encoding.rb:33:in `encoding=': No decoder found for encoding ASCII. Please install iconv. (Exception) from c:/ruby/lib/ruby/1.8/rexml/source.rb:40:in`encoding=’  
from c:/ruby/lib/ruby/1.8/rexml/parsers/baseparser.rb:202:in `pull' from c:/ruby/lib/ruby/1.8/rexml/parsers/treeparser.rb:21:in`parse’  
from c:/ruby/lib/ruby/1.8/rexml/document.rb:176:in `build' from c:/ruby/lib/ruby/1.8/rexml/document.rb:45:in`initialize’  
from C:/Documents and Settings/hwalker/Desktop/Ruby-1.rb:13:in `new’  
from C:/Documents and Settings/hwalker/Desktop/Ruby-1.rb:13
