creating a openstruct from xml

R

Rajinder Yadav

Hi just wondering if there is a easy way to turn an xml entity into a
object like of type OpenStruct where i can access everything like a
property?

It's kind of like parsing xml, I already know about nokogiri, but I want
to work is a OpenStruct and not a DOM object, does that make sense? =P

--
Kind Regards,
Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1
 
J

James Edward Gray II

Hi just wondering if there is a easy way to turn an xml entity into a =
object like of type OpenStruct where i can access everything like a =
property?
=20
It's kind of like parsing xml, I already know about nokogiri, but I =
want to work is a OpenStruct and not a DOM object, does that make sense? =
=3DP

Check out XmlSimple:

http://xml-simple.rubyforge.org/

You can pass the Hashes it returns to OpenStruct.

You may also want to see the second example in these slides for a much =
more evil idea:

http://www.slideshare.net/JamesEdwardGrayII/unblocked

James Edward Gray II=
 
R

Rajinder Yadav

to work is a OpenStruct and not a DOM object, does that make sense? =3DP
Check out XmlSimple:

=A0http://xml-simple.rubyforge.org/

You can pass the Hashes it returns to OpenStruct.

You may also want to see the second example in these slides for a much mo= re evil idea:

=A0http://www.slideshare.net/JamesEdwardGrayII/unblocked

James Edward Gray II

James,

thanks, xml-simple does what I was looking for. Do you know if there
is a similar thing that can be done with nokogiri?

--=20
Kind Regards,
Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-22-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.1
 
J

James Edward Gray II

a object like of type OpenStruct where i can access everything like a =
property?want to work is a OpenStruct and not a DOM object, does that make sense? =
=3DP
=20
James,
=20
thanks, xml-simple does what I was looking for. Do you know if there
is a similar thing that can be done with nokogiri?

Not that I know of. You would need to write the code that makes the =
translation.

James Edward Gray II=
 
M

Mike Dalessio

[Note: parts of this message were removed to make it a legal post.]

James,

thanks, xml-simple does what I was looking for. Do you know if there
is a similar thing that can be done with nokogiri?


You may want to check out Nokogiri's "slop" mode (invented by the
inestimable John Barnette):

require 'rubygems'
require "nokogiri"

doc = Nokogiri::Slop <<-EOXML
<employees>
<employee status="active">
<fullname>Dean Martin</fullname>
</employee>
<employee status="inactive">
<fullname>Jerry Lewis</fullname>
</employee>
</employees>
EOXML

# navigate!
doc.employees.employee.last.fullname.content # => "Jerry Lewis"

# access node attributes!
doc.employees.employee.first["status"] # => "active"

# use some xpath!
doc.employees.employee("[@status='active']").fullname.content # => "Dean
Martin"
doc.employees.employee:)xpath => "@status='active'").fullname.content #
=> "Dean Martin"

# use some css!
doc.employees.employee("[status='active']").fullname.content # => "Dean
Martin"
doc.employees.employee:)css => "[status='active']").fullname.content #
=> "Dean Martin"
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top