2

I have looked everywhere I think, but not found an answer.

I am consuming a SOAP API and I wish to store only part of the response in the database as XML.

The code is as follows:

require 'rexml/document'
doc = REXML::Document.new(response.to_xml)
data = doc.root.elements['//SearchResult'].to_s

This gives me all the XML inside the node of my response.

I want only the contents of that node, not the node.

Right now I get:

<SearchResult>
    <bla></bla>
    <bla2></bla2>
</SearchResult>

But I only want:

<bla></bla>
<bla2></bla2>

I'm using ruby 1.9.3-head with Rails 3.2.x.

I found a .value() method somewhere but that doesn't work on elements, which is what I get from the XPath search.

Please advise.

vvohra87
  • 5,594
  • 4
  • 22
  • 34

1 Answers1

2
doc.root.elements['//SearchResult'].elements.each do | elem |
  p elem
end

gives

<bla> ... </>
<bla2> ... </>

So, with

data = doc.root.elements['//SearchResult'].elements.map(&:to_s)

you can retrieve an array of String representations of all sub-nodes.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • Nice one. I had thought of this but I kept looking for conventional 'innerXML' or 'value' methods from the source lib. For the sake of expedience, I'm going to presume this is the only way :) – vvohra87 Apr 03 '12 at 12:42