something similar to perl's XML::Simple

C

Cam

Hi!

I'm trying to write a few simple apps (a flash-card type program for
chinese characters, and a simple address book type program), and for
the file format i want to use XML. Anyway, i don't need anything
complex at all... just the most basic reading/writing of xml files
possible. i was looking through a bunch of the various ruby-xml
libraries, and it seems that rexml is the 'official' xml library for
ruby? I like the looks of it, but it seems overly complex for what i
want to do. My friend steve told me about a module in perl called
xml:simple (apparently it just loads up the xml file into a nice
little hash for you). Anyway, that sounds like just the thing for me,
and i was wondering if ruby had anything similar. If not maybe just
some simple example code for rexml (the debian packages didnt' seem to
come w/ any). Tips or suggestions on xm file i/o would be greatly
appreciated.

thanks,
Cameron Matheson
 
J

James Edward Gray II

Hi!

I'm trying to write a few simple apps (a flash-card type program for
chinese characters, and a simple address book type program), and for
the file format i want to use XML. Anyway, i don't need anything
complex at all... just the most basic reading/writing of xml files
possible. i was looking through a bunch of the various ruby-xml
libraries, and it seems that rexml is the 'official' xml library for
ruby? I like the looks of it, but it seems overly complex for what i
want to do. My friend steve told me about a module in perl called
xml:simple (apparently it just loads up the xml file into a nice
little hash for you). Anyway, that sounds like just the thing for me,
and i was wondering if ruby had anything similar. If not maybe just
some simple example code for rexml (the debian packages didnt' seem to
come w/ any). Tips or suggestions on xm file i/o would be greatly
appreciated.

Are you strictly tied to the XML idea? YAML is a standard Ruby
library and pretty darn easy to use. Here's an example:

$ ls
yaml_example.rb
$ cat yaml_example.rb
#!/usr/local/bin/ruby -w

require "YAML"

unless File.exists? "contacts.yaml" # save some YAML
# build up some example data...
require "ostruct"

contacts = Hash.new

contact = OpenStruct.new
contact.first_name = "James"
contact.last_name = "Gray"
contact.address = "Where Ever Dr."
contact.state = "Edmond"
contact.city = "OK"
contact.zip = "12345"

contacts["#{contact.last_name}, #{contact.first_name}"] =
contact

contact = contact.dup
contact.first_name = "Dana"

contacts["#{contact.last_name}, #{contact.first_name}"] =
contact

# store data in a YAML file
File.open("contacts.yaml", "w") { |file| YAML.dump(contacts,
file) }
else # reload YAML data
# load
contacts = File.open("contacts.yaml") { |file| YAML.load
(file) }

# and use...
p contacts["Gray, James"].first_name
end
$ ruby yaml_example.rb
$ ls
contacts.yaml yaml_example.rb
$ cat contacts.yaml
---
"Gray, James": !ruby/object:OpenStruct
table:
:city: OK
:first_name: James
:zip: "12345"
:last_name: Gray
:address: Where Ever Dr.
:state: Edmond
"Gray, Dana": !ruby/object:OpenStruct
table:
:city: OK
:first_name: Dana
:last_name: Gray
:zip: "12345"
:address: Where Ever Dr.
:state: Edmond
$ ruby yaml_example.rb
"James"

If you really do need XML, REXML is the way to go, in my opinion.
It's not too hard to learn and it will grow with you. We've had a
few Ruby Quizzes that dealt (in part) with XML writing and parsing.
Scanning those solutions will show you examples. Here are some links...

For writing XML see Hans Fugal's and Brian Kang's solutions to:

http://www.rubyquiz.com/quiz6.html

For reading XML see most of the solutions to:

http://www.rubyquiz.com/quiz30.html

Now to finally answer your actual question: Yes, we do have a port
of XML::Simple. You can find the Ruby version here:

http://www.maik-schmidt.de/xml-simple.html

Hope that helps.

James Edward Gray II
 
C

Cam

Thanks everyone,

I will check out the port for XML simple, that is just what i was
looking for... i will need to look further into YAML however... i've
never heard of that one before. I'm not necessarily needing XML per
se (i just thought it seemed like a convenient way to store my data,
but YAML seems to provide the same level of convenience). Is that a
standard format across many languages (i hadn't heard of it before).

Anyway, thanks for the help guys,
Cameron Matheson

On Jul 22, 2005, at 12:36 PM, Cam wrote:
=20
Hi!

I'm trying to write a few simple apps (a flash-card type program for
chinese characters, and a simple address book type program), and for
the file format i want to use XML. Anyway, i don't need anything
complex at all... just the most basic reading/writing of xml files
possible. i was looking through a bunch of the various ruby-xml
libraries, and it seems that rexml is the 'official' xml library for
ruby? I like the looks of it, but it seems overly complex for what i
want to do. My friend steve told me about a module in perl called
xml:simple (apparently it just loads up the xml file into a nice
little hash for you). Anyway, that sounds like just the thing for me,
and i was wondering if ruby had anything similar. If not maybe just
some simple example code for rexml (the debian packages didnt' seem to
come w/ any). Tips or suggestions on xm file i/o would be greatly
appreciated.
=20
Are you strictly tied to the XML idea? YAML is a standard Ruby
library and pretty darn easy to use. Here's an example:
=20
$ ls
yaml_example.rb
$ cat yaml_example.rb
#!/usr/local/bin/ruby -w
=20
require "YAML"
=20
unless File.exists? "contacts.yaml" # save some YAML
# build up some example data...
require "ostruct"
=20
contacts =3D Hash.new
=20
contact =3D OpenStruct.new
contact.first_name =3D "James"
contact.last_name =3D "Gray"
contact.address =3D "Where Ever Dr."
contact.state =3D "Edmond"
contact.city =3D "OK"
contact.zip =3D "12345"
=20
contacts["#{contact.last_name}, #{contact.first_name}"] =3D
contact
=20
contact =3D contact.dup
contact.first_name =3D "Dana"
=20
contacts["#{contact.last_name}, #{contact.first_name}"] =3D
contact
=20
# store data in a YAML file
File.open("contacts.yaml", "w") { |file| YAML.dump(contacts,
file) }
else # reload YAML data
# load
contacts =3D File.open("contacts.yaml") { |file| YAML.load
(file) }
=20
# and use...
p contacts["Gray, James"].first_name
end
$ ruby yaml_example.rb
$ ls
contacts.yaml yaml_example.rb
$ cat contacts.yaml
---
"Gray, James": !ruby/object:OpenStruct
table:
:city: OK
:first_name: James
:zip: "12345"
:last_name: Gray
:address: Where Ever Dr.
:state: Edmond
"Gray, Dana": !ruby/object:OpenStruct
table:
:city: OK
:first_name: Dana
:last_name: Gray
:zip: "12345"
:address: Where Ever Dr.
:state: Edmond
$ ruby yaml_example.rb
"James"
=20
If you really do need XML, REXML is the way to go, in my opinion.
It's not too hard to learn and it will grow with you. We've had a
few Ruby Quizzes that dealt (in part) with XML writing and parsing.
Scanning those solutions will show you examples. Here are some links...
=20
For writing XML see Hans Fugal's and Brian Kang's solutions to:
=20
http://www.rubyquiz.com/quiz6.html
=20
For reading XML see most of the solutions to:
=20
http://www.rubyquiz.com/quiz30.html
=20
Now to finally answer your actual question: Yes, we do have a port
of XML::Simple. You can find the Ruby version here:
=20
http://www.maik-schmidt.de/xml-simple.html
=20
Hope that helps.
=20
James Edward Gray II
=20
=20
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top