xml

H

Harish Dewangan

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 :
(e-mail address removed)

Thanks in Advance
Harish
 
B

Brian Candler

Harish said:
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.
 
H

Harish Dewangan

Brian said:
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.


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
 
B

Brian Candler

Harish said:
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.
 
J

Jarmo Pertman

Brian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top