Parsing XML in Ruby

A

Adam Lorens

Bertram said:
Hi,

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

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
 
B

Bertram Scharpf

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
 
A

Adam Lorens

Bertram said:
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


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

brabuhr

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'
=3D> true
irb(main):002:0> file =3D File.new('sample.xml')
=3D> #<File:sample.xml>
irb(main):003:0> doc =3D REXML::Document.new(file)
=3D> <UNDEFINED> ... </>
irb(main):004:0> root =3D doc.root
=3D> <env:Envelope xmlns:env=3D'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 =3D root.elements['env:Body']
=3D> <env:Body> ... </>
irb(main):009:0> acr =3D body.elements['ns2:getActiveChannelsResponse']
=3D> <ns2:getActiveChannelsResponse xmlns:ns2=3D'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.
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 22. Jul 2009, 19:15:00 +0900 schrieb Adam Lorens:
Bertram said:
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( ..."?
^^^^^

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
 
A

Adam Lorens

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top