[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: humanized Xml tree navigation

Bob Hutchison

8/1/2006 12:50:00 PM

Hi,

On Jul 31, 2006, at 9:43 AM, NAKAMURA, Hiroshi wrote:
> 0% cat people.xml
> <people>
> <person>
> <name>name1</name>
> <size>1</size>
> </person>
> <person>
> <name>name2</name>
> <size>2</size>
> </person>
> <person>
> <name>name3</name>
> <size>3</size>
> </person>
> </people>
> 0% irb
> irb(main):001:0> require 'xsd/mapping'
> => true
> irb(main):002:0> people = XSD::Mapping.xml2obj(File.read
> ("people.xml"))
> => #<SOAP::Mapping::Object:0x3592020
> {}person=[#<SOAP::Mapping::Object:0x3591ea0 {}name="name1" {}
> size="1">,
> #<SOAP::Mapping::Object:0x3591684 {}name="name2" {}size="2">,
> #<SOAP::Mapping::Object:0x3590f7c {}name="name3" {}size="3">]>
> irb(main):003:0> people.person.each { |person| p person }
> #<SOAP::Mapping::Object:0x3591ea0 {}name="name1" {}size="1">
> #<SOAP::Mapping::Object:0x3591684 {}name="name2" {}size="2">
> #<SOAP::Mapping::Object:0x3590f7c {}name="name3" {}size="3">
> => [#<SOAP::Mapping::Object:0x3591ea0 {}name="name1" {}size="1">,
> #<SOAP::Mapping::Object:0x3591684 {}name="name2" {}size="2">,
> #<SOAP::Mapping::Object:0x3590f7c {}name="name3" {}size="3">]
> irb(main):004:0> people.person[2].name
> => "name3"
> irb(main):005:0> people.person.name
> NoMethodError: undefined method `name' for #<Array:0x6b22108>
> from (irb):5
> irb(main):006:0>
>
> Almost.
>

Nice! I didn't know about this.

Loses child ordering though, mixed content, and the root element?

Both of the following lines:

puts XSD::Mapping.obj2xml(XSD::Mapping.xml2obj("<p><a/><b/><a/><b/></
p>"))
puts XSD::Mapping.obj2xml(XSD::Mapping.xml2obj("<p>1<a/>2<b/>3<a/>4<b/
>5</p>"))

give the same output:

<?xml version="1.0" encoding="utf-8" ?>
<SOAP..Mapping..Object>
<a>
</a>
<a>
</a>
<b>
</b>
<b>
</b>
</SOAP..Mapping..Object>

Cheers,
Bob

----
Bob Hutchison -- blogs at <http://www.rec...
hutch/>
Recursive Design Inc. -- <http://www.rec...>
Raconteur -- <http://www.raconteur...
xampl for Ruby -- <http://rubyforge.org/projects/...




1 Answer

Bob Hutchison

8/8/2006 12:13:00 PM

0


On Aug 7, 2006, at 3:09 AM, NAKAMURA, Hiroshi wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
>
> Bob Hutchison wrote:
>> Loses child ordering though, mixed content, and the root element?
>
> Yes, Yes and Yes. obj2xml and xml2obj is not for round-trip
> conversion
> although its name indicates it.

I understand why you did this a little better after playing around
with the Flickr API yesterday.

>
> I introduced this XML <-> Ruby mapping to avoid bothersome XML DOM
> programming in Web Services (soap4r). Do you think xampl can help us
> instead of XSD::Mapping?
>

[snip]

> Creating XML request with a Hash and creating Ruby object with
> XSD::Mapping. Both are for XML <-> Ruby mapping. It's enough
> small but
> when I come to create more complexed XML instance... How I can write
> this with xampl?

Xampl is definitely targeted at XML that you want to convert into an
object model. But I have used it for simply removing the DOM from
manipulating XML.

I had a look at the Flickr API yesterday. I don't know much about
Flickr, I'm not a regular user of it. And I've never seen the API
before yesterday. So it seems that I don't have a clear idea what
people do with the API. That leaves me thrashing around a bit. Any
suggestions as to what people, like you perhaps, want to do with the
API?

Anyway I pretended I had a clue and carried on regardless. Here's
where I got.

Xampl uses example documents to build Ruby objects that can hold any
of the example documents or any document 'similar' to the combined
examples. The Flickr API is SOAP (even the 'REST' API might as well
be SOAP) which means that the responses are not in general complete
representations of something useful (to xampl), they are fragments of
that. I'm not editorialising on SOAP here, but this explains the next
thing I did. So I started looking around for objects that were either
returned, or in desperation, could be constructed without having to
authenticate (I didn't have the energy to read and figure out how
that works, yesterday was a holiday in Canada and I wanted to keep it
that way :-) Anyhow I settled on the 'Photo' (seems appropriate :-)

I figured I could get a fairly complete Photo object together by
using three API calls:

flickr.photos.getInfo -- this gets an XML response that describes a
Photo
flickr.photos.getAllContexts -- this gets pools and groups of the photo
flickr.photos.comments.getList -- this gets all the comments of the
photo

And to get a list of photos to play with I used
flickr.interestingness.getList which returns the first N
'interesting' photos.

So anyway I got a few examples of each and ran xampl on the collection.

Then I wrote a short program to grab the interesting list, and build
up the objects with info, groups, pools, and comments. And made them
persistent.

Now all I need to know is what to do with these photo objects???

Cheers,
Bob

>
> Regards,
> // NaHi

----
Bob Hutchison -- blogs at <http://www.rec...
hutch/>
Recursive Design Inc. -- <http://www.rec...>
Raconteur -- <http://www.raconteur...
xampl for Ruby -- <http://rubyforge.org/projects/...