Does REXML support to_json? I keep getting "object references itself" when I do this (simplified version): xml = Document.new("<root><ooh fert=''nerdle''>Narf!</ooh></root>") @json = xml.to_json Is it possible to output this xml as json. The source xml is actually much larger, and I want to execute some xpath on that, and return the results as json. -S --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 24, 2008 5:38 PM, Stuart Grimshaw <stuart.grimshaw-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Does REXML support to_json? I keep getting "object references itself" > when I do this (simplified version): > > xml = Document.new("<root><ooh fert=''nerdle''>Narf!</ooh></root>") > > @json = xml.to_json > > Is it possible to output this xml as json. > > The source xml is actually much larger, and I want to execute some > xpath on that, and return the results as json.Apparently not, but you can probably use Hash.from_xml, which parses the XML into a hash using REXML and SimpleXML. hash = Hash.from_xml("...") json = hash.to_json -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > Is it possible to output this xml as json. > > > The source xml is actually much larger, and I want to execute some > > xpath on that, and return the results as json. > > Apparently not, but you can probably use Hash.from_xml, which parses > the XML into a hash using REXML and SimpleXML. > > hash = Hash.from_xml("...") > json = hash.to_jsonCheers Rick, That works, trying to do the same with xml.elements(xpath) introduces a new problem though, I feel I''m close to working it out, but it''s late, and I''m going to bed. I''m trying to take a fairly large xml document (from a 3rd party url), run an xpath query on it, then return those results as json to my client side js. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 25, 11:52 pm, Stuart Grimshaw <stuart.grims...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to take a fairly large xml document (from a 3rd party url), > run an xpath query on it, then return those results as json to my > client side js.I''ve been trying this approach: sourceXml = Document.new(lots of xml) xml = Document.new(''<data/>'') xml.entities.each(xpath) { |e| xml.root.add e } Which works, but takes a very long time to complete. Is this the most efficient way to do it? I might just create a hash right there, from the attributes I''m interested in, rather than send the whole lot back. -S --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I''ve been trying this approach: > > sourceXml = Document.new(lots of xml) > > xml = Document.new(''<data/>'') > > xml.entities.each(xpath) { |e| > xml.root.add e > > } > > Which works, but takes a very long time to complete. Is this the most > efficient way to do it? I might just create a hash right there, from > the attributes I''m interested in, rather than send the whole lot back.In the end I ditched rexml and went with the ruby libxml bindings to speed things up. Then built a hash up with the results like this: @i = Hash.new elem = xml.find(xpath).each do |e| messageId = e.parent.parent[''message_id''] @i[messageId] = Hash.new @i[messageId][''summary''] = e.find_first(''./element'')[''summary''] . . end The just call to_json on @i Hopefully this will be usefull to anyone else doing the same kind of thing. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---