Something similar to perl's XML::Simple

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

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

···

On Jul 22, 2005, at 12:36 PM, Cam wrote:

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.

---
"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

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 7/22/05, James Edward Gray II <james@grayproductions.net> wrote:

On Jul 22, 2005, at 12:36 PM, Cam wrote:

> 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

Yes. For more information see:

http://www.yaml.org/

James Edward Gray II

···

On Jul 22, 2005, at 1:47 PM, Cam wrote:

Is that a standard format across many languages (i hadn't heard of it before).