SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: XML Mapping

  1. #1
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML Mapping

    I thought I'd start a small thread to give a bit of publicity to the excellent Ruby XML-Mapping library - I've not seen it mentioned often and its quite low down on the RubyForge listings - a shame, as it is a great way of working with XML.

    Ruby has a great XML parser in the form of REXML. However, even with the greatest of XML parsers, XML parsing can be incredibly tedious, especially when you are working with the same XML data/schema on a regular basis. XML-Mapping solves this problem by enabling you to create native Ruby objects that map directly to your XML schema. Kind of like ActiveRecord for XML, thought not quite as "automatic" as ActiveRecord - a bit of config is still needed.

    Here is a brief example. XML nodes can be mapped to native Ruby objects, including your own custom objects.

    Code:
    #--data.xml
      <person id="001">
        <name>Luke Redpath</name>
        <job>Web Developer</job>
        <address>
          <street>Some Street</street>
          <city>Birmingham</city>
          <postcode>BH1 23X</postcode>
        </address>
      </person>
    Now, to create our objects, we simply create the class definitions, include the XML-mapping library, and set out our mappings. Going by the example above, I'd say we have two custom objects, Person and Address, and the rest can be mapped to native types (people is an array, everything else is a string).

    Code:
    require 'xml/mapping'
    
    # forward declarations
    class Person; end;
    class Address; end;
    
    class Person
      include XML::Mapping
    
      numeric_node  :id, "@id"
      text_node  :name, "name"
      text_node  :job, "job"
      object_node  :address, "address", :class => Address
    end
    
    class Address
      include XML::Mapping
      
      text_node :street, "street"
      text_node :city, "city",
      text_node :postcode, "postcode"
    end
    
    # Example usage: 
    person = Person.load_from_file("data.xml")
    puts person.name
    => "Luke Redpath"
    puts person.address.street
    => "Some Street"
    For more examples and the api:
    http://xml-mapping.rubyforge.org/

    It can be installed as a gem:
    gem install xml-mapping

    Things that could be improved? It would be really cool if it could automatically determine the mappings from an XSD schema file if an XML file had one associated with it. That would make it as awesome as ActiveRecord.

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Nice. Seems like a quick jumpstart to an RSS transformation library

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Indeed - you could simply write prebuilt schema definitions by extending the XML::Mapping class.

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Yakima WA.
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Nice. Seems like a quick jumpstart to an RSS transformation library
    Ruby also has an RSS parser built into the standard library fyi:

    PHP Code:
    require 'rss/1.0'
    require 'open-uri'

    open('http://example.com/rss.xml') do |http|
      
    response http.read
      result 
    RSS::Parser.parse(responsefalse)
      
    puts "Channel: #{result.channel.title}"
      
    result.items.each do |item|
        
    puts item.title
      end
    end 

    It can also generate RSS feeds...
    Cheers...

  5. #5
    SitePoint Addict Brak's Avatar
    Join Date
    Jul 2004
    Location
    Central Coast, CA
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I'd really love to see is an ActiveRecord-esk XML parsing library. One that assumes reasonable defaults and can more or less do all of this "automatically" for you. Simply do something like:

    Code:
    Feed = XMLSuperLib::load(rss.xml)
    
    for person in Feed.person
      puts person.name
    end
    Studio Rockstar's Blog - A journey to quitting the dayjob.

  6. #6
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    Things that could be improved? It would be really cool if it could automatically determine the mappings from an XSD schema file if an XML file had one associated with it. That would make it as awesome as ActiveRecord.
    That would be hard and not that usefull, because a schema hardly ever changes, and you ussually write the description once. And parsing a DTD or a XSD schema is hard, the implementation is definitelly slow, and besides, they could contain incomplete information, because you can define tags that accept annonymous tags anytime (in fact it's a common practice to define only really important parts of the XML structure).
    Last edited by bonefry; Sep 30, 2005 at 17:56.

  7. #7
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PS: I know you guys are all excited and all about Ruby, and this class is really cool (resembling ActiveRecord), but I will still use a DOM api, because I am familliar with it, and I can migrate the resulting script anytime to PHP,Java and JavaScript with only minor syntax changes. And once you get familliar with it, it's pretty simple to use.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    METROmilwaukee
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get Xml-Mapping Library Here...

    [QUOTE=Luke Redpath]
    <snip />

    // May not load...
    // For more examples and the api:
    // http://xml-mapping.rubyforge.org/

    // This page loads directly...
    http://rubyforge.org/projects/xml-mapping/

    Thanks for bringing this to our attention Luke


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •