Parsing XML in Ruby

Hi!

I’m new in this forum, so i’m be very gratefull, you won’t be angry if I
ask some stupid question. My today work is: “Parsing XML in Ruby”, XML
is in attachment. I’ve never done it, so please help me:)

thanks

Adam

Check out hpricot: http://wiki.github.com/why/hpricot

Regards,
Milan

Milan D. wrote:

Check out hpricot: http://wiki.github.com/why/hpricot

Regards,
Milan

thank you very much Milan, I found this web, but I’m still be very
gratefull if someone write even small part of this.

Here are the hpricot examples:

http://wiki.github.com/why/hpricot/hpricot-xml

Regards,
Milan D.

Hi,

Am Dienstag, 21. Jul 2009, 23:39:14 +0900 schrieb Adam Giewond:

I’m new in this forum, so i’m be very gratefull, you won’t be angry if I
ask some stupid question. My today work is: “Parsing XML in Ruby”, XML
is in attachment. I’ve never done it, so please help me:)

REXML belongs to the Ruby standard library.

http://www.xml.com/pub/a/2005/11/09/rexml-processing-xml-in-ruby.html

Bertram

Bertram S. wrote:

Hi,

Am Dienstag, 21. Jul 2009, 23:39:14 +0900 schrieb Adam Giewond:

I’m new in this forum, so i’m be very gratefull, you won’t be angry if I
ask some stupid question. My today work is: “Parsing XML in Ruby”, XML
is in attachment. I’ve never done it, so please help me:)

REXML belongs to the Ruby standard library.

http://www.xml.com/pub/a/2005/11/09/rexml-processing-xml-in-ruby.html

Bertram

ok thanks very much, I decided to use REXML, but i have problem with
this examples. My XML file is more complicated so I want require to
simplifi code. I’m still using this example (movies.xml) becouse it’s
less complicated. I don’t want refer to every position (like title, year
etc) but to all. My proposition:

#!/usr/bin/ruby -w

require ‘rexml/document’
include REXML

xmlfile = File.new(“movies.xml”)
xmldoc = Document.new(xmlfile)

Now get the root element

root = xmldoc.root
puts "Root element : " + root.attributes[“shelf”]

This will output all the movie titles.

xmldoc.elements.each(“collection”){
|e| puts e.attributes[“title”]
}

doesn’t work.

I’ll be very gratefull when someone explain where is mistake…

regards
adam

Bertram S. wrote:

Hi,

Am Mittwoch, 22. Jul 2009, 17:43:09 +0900 schrieb Adam Lorens:

xmlfile = File.new(“movies.xml”)
xmldoc = Document.new(xmlfile)

This will output all the movie titles.

xmldoc.elements.each(“collection”){
|e| puts e.attributes[“title”]
}

Did you try “xmldoc.root.elements.each( …”?
^^^^^

Bertram

Hi,

I try Your proposition, but it also doesn’t work, because SciTe only
says:
“Root element: New arrivals”( I want more information which is on
XML).My new XML is in attachment.

regards
Adam

Hi,

Am Mittwoch, 22. Jul 2009, 17:43:09 +0900 schrieb Adam Lorens:

xmlfile = File.new(“movies.xml”)
xmldoc = Document.new(xmlfile)

This will output all the movie titles.

xmldoc.elements.each(“collection”){
|e| puts e.attributes[“title”]
}

Did you try “xmldoc.root.elements.each( …”?
^^^^^

Bertram

On Wed, Jul 22, 2009 at 4:43 AM, Adam Lorens[email protected] wrote:

http://www.xml.com/pub/a/2005/11/09/rexml-processing-xml-in-ruby.html

Bertram

ok thanks very much, I decided to use REXML, but i have problem with
this examples.

Here are two small examples based on your original XML:

irb(main):001:0> require ‘rexml/document’
=> true
irb(main):002:0> file = File.new(‘sample.xml’)
=> #<File:sample.xml>
irb(main):003:0> doc = REXML::Document.new(file)
=> … </>
irb(main):004:0> root = doc.root
=> <env:Envelope xmlns:env=‘http://schemas.xmlsoap.org/soap/envelope/’>
… </>

irb(main):005:0> root.each_element(’//ns2:sourcingChannel’) do |sC|
irb(main):006:1* puts sC.elements[‘name’].text
irb(main):007:1> end
United Utilities
testttttttttttt
Broadbean AdCourier - NEW
Monster
Pracuj.pl
momozozo
Vanilla
. . .

irb(main):008:0> body = root.elements[‘env:Body’]
=> env:Body … </>
irb(main):009:0> acr = body.elements[‘ns2:getActiveChannelsResponse’]
=> <ns2:getActiveChannelsResponse xmlns:ns2=‘http://ws.mrted.com/’> …
</>
irb(main):010:0> acr.elements.each do |sC|
irb(main):011:1* puts sC.elements[‘name’].text
irb(main):012:1> end
United Utilities
N Power
mzych.com
HPA
gttt
Vanilla
FCO
Lloyds TSB Tel Banking OTU
. . .

irb(main):013:0> root.each_element(’//ns2:sourcingChannel/name’) do
|name|
irb(main):014:1* puts name.text
irb(main):015:1> end
testttttttttttt
Broadbean AdCourier - NEW
Monster
Pracuj.pl
momozozo
Broadbean AdCourierx
Vanilla
. . .

I usually use something like the first way: each_element with an XPath
to pull out the “record” nodes for processing.

Hi,

Am Mittwoch, 22. Jul 2009, 19:15:00 +0900 schrieb Adam Lorens:

Did you try “xmldoc.root.elements.each( …”?
^^^^^

I try Your proposition, but it also doesn’t work, because SciTe only
says:
“Root element: New arrivals”( I want more information which is on
XML).My new XML is in attachment.

Attachments:
http://www.ruby-forum.com/attachment/3893/movies.rb

Could you provide the movies.xml file in any way?

Bertram

I forgot add, It is right way?

hi,
thank you very much, first way is correct, and I’ll use it. but I have
another question. I’m looking for assert, does this element have
descendant, and if exist, does exist assert scheme. So I get xml
element, first I check does he has children, and what this children look
like.

regards
Adam

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs