Xml

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 = "
123
FirstSecondThird
FirstSecondThird
FirstSecondThird
"
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 :
[email protected]

Thanks in Advance
Harish

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.

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

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:

foo bar baz ...

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.

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