Forum: Ruby xml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
xml
Harish D. (Guest)
on 2009-01-26 13:33
Hi All,

    i am new ruby on rails n i am currently working on xml with Ruby on
rails.

    i prepared string and passed that string into simplexml module
 xml_data = "<records>
              <record><first>1</first><second>2</second><third>3</third></record>
<arn><child1>First</child1><child2>Second</child2><child3>Third</child3></arn>
              <arn><child1>First</child1><child2>Second</child2><child3>Third</child3></arn>
                  <arn><child1>First</child1><child2>Second</child2><child3>Third</child3></arn>
                    </records>"
 data = XmlSimple.xml_in(xml_data)

 i got pure xml now and converting that xml into model object and when i
am trying to save the object it is inserting with the junk values as
shown below

'---- "1"', '--- - "2"', '--- - "3"', '--- - First', '--- - Second',
'--- - third'

is any one can help me plz whats the prb here ..
if you have any solution can you send me plz on my id :
removed_email_address@domain.invalid

Thanks in Advance
Harish
Brian C. (Guest)
on 2009-01-26 16:34
Harish Dewangan wrote:
>     i am new ruby on rails

...

>  i got pure xml now and converting that xml into model object and when i
> am trying to save the object it is inserting with the junk values as
> shown below

Problems with Rails and/or ActiveRecord would best be asked on a Rails
mailing list (here you have found the general Ruby programming language
list)

From a Ruby point of view, I'd suggest you try using 'inspect' on your
model before saving it. e.g.

  # create your model
  foo = Foo.new(...)

  # show it
  STDERR.puts foo.inspect

  foo.save!

This divides your problem in half: if @attributes contains the right
data then the problem is when ActiveRecord saves it, but if @attributes
contains the bad data then you can work on the first part of your
program.

> '---- "1"', '--- - "2"', '--- - "3"', '--- - First', '--- - Second',
> '--- - third'

Strange, looks like it's converting to YAML for some reason.
Harish D. (Guest)
on 2009-01-27 16:16
Brian C. wrote:
> Harish Dewangan wrote:
>>     i am new ruby on rails
>
> ...
>
>>  i got pure xml now and converting that xml into model object and when i
>> am trying to save the object it is inserting with the junk values as
>> shown below
>
> Problems with Rails and/or ActiveRecord would best be asked on a Rails
> mailing list (here you have found the general Ruby programming language
> list)
>
> From a Ruby point of view, I'd suggest you try using 'inspect' on your
> model before saving it. e.g.
>
>   # create your model
>   foo = Foo.new(...)
>
>   # show it
>   STDERR.puts foo.inspect
>
>   foo.save!
>
> This divides your problem in half: if @attributes contains the right
> data then the problem is when ActiveRecord saves it, but if @attributes
> contains the bad data then you can work on the first part of your
> program.
>
>> '---- "1"', '--- - "2"', '--- - "3"', '--- - First', '--- - Second',
>> '--- - third'
>
> Strange, looks like it's converting to YAML for some reason.

Sir,

when i add the Code what you gave like STDERR.puts foo.inspect
it printed like this, i think this is correct data.
#<XMLDemo first: ["1"], second: ["2"], third: ["3"], child1: ["First"],
child2: ["Second"], child3: ["Third"], id: nil>

But still i am facing the same problem. plz help to solve this prb

Thanks in Advance
Harish Kumar Dewangan
Brian C. (Guest)
on 2009-01-27 17:00
Harish Dewangan wrote:
> when i add the Code what you gave like STDERR.puts foo.inspect
> it printed like this, i think this is correct data.
> #<XMLDemo first: ["1"], second: ["2"], third: ["3"], child1: ["First"],
> child2: ["Second"], child3: ["Third"], id: nil>

No, that's wrong, and it explains why your data is getting serialized as
YAML.

Each of your attributes is an array:

  ["1"]

when it should be just a string

  "1"

XmlSimple is giving you an array because you might get multiple elements
with the same name:

<record>
  <first>foo</first>
  <first>bar</first>
  <first>baz</first>
  ...
</record>

So your code which copies from your XML structure into your ActiveRecord
model needs changing, e.g. by using .first or [0] to pick the first
array element.

I have a vague recollection that there's an option you can pass to
XmlSimple for it *not* to put elements inside arrays; look through the
docs.
Jarmo P. (Guest)
on 2009-01-31 01:02
Brian C. wrote:
> I have a vague recollection that there's an option you can pass to
> XmlSimple for it *not* to put elements inside arrays; look through the
> docs.

You are correct. I just had similar problem as OT.
The option is 'ForceArray' => false
This topic is locked and can not be replied to.