# Xml

**URL:** <https://www.ruby-forum.com/t/xml/158649>\
**Category:** Ruby\
**Created:** [January 26, 2009, 12:33pm UTC](https://www.ruby-forum.com/t/xml/158649 "2009-01-26T12:33:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harish\_dew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@harish\_dew](https://www.ruby-forum.com/u/harish_dew)\
**Post date:** [January 26, 2009, 12:33pm UTC](https://www.ruby-forum.com/t/xml/158649/1 "2009-01-26T12:33:32Z")

</div>

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 :  
removed\_email\_address@domain.invalid

Thanks in Advance  
Harish

---

<div class="post-metadata">

**Author:** ![harish\_dew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@harish\_dew](https://www.ruby-forum.com/u/harish_dew)\
**Post date:** [January 26, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/xml/158649/2 "2009-01-26T15:34:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harish\_dew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@harish\_dew](https://www.ruby-forum.com/u/harish_dew)\
**Post date:** [January 27, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/xml/158649/3 "2009-01-27T15:16:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harish\_dew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@harish\_dew](https://www.ruby-forum.com/u/harish_dew)\
**Post date:** [January 27, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/xml/158649/4 "2009-01-27T16:00:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harish\_dew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@harish\_dew](https://www.ruby-forum.com/u/harish_dew)\
**Post date:** [January 31, 2009, 12:02am UTC](https://www.ruby-forum.com/t/xml/158649/5 "2009-01-31T00:02:08Z")

</div>

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
