Forum: Ruby Generating an XML file

0faae51834740c2142efb5af4b8d8175?d=identicon&s=25 Rochit Sen (rochitsen)
on 2013-11-04 09:51
Hi All,
I am reading an excel file and then generating a XML file. I am using
the below code. I am using Builder to do soo. Below i thought that as i
have a 'x' object of builder, i can write it to a yaml file using the
dump method.

CODE:
def generate_xml()
 x = Builder::XmlMarkup.new(:target => $stdout, :indent => 1)
 x.instruct!
 x.comment! "A test xml for test cases"
 x.FileName "#{@p}"
 x.TotalCases "#{@t}"
 x.Contents "#{@c}"
 File.open('dump.xml','w') {|f| f.write(YAML.dump(x))}
 puts "generated xml" #Just to print this in console
end

When i run below i get error -
C:/Ruby193/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:413:in
`dump_coder': undefined method `name' for #<IO:<STDOUT>> (NoMethodError)

OR what could be another approach to generate an xml file?

Thanks
C993c6aaffc6cf7b477a5075099e1d36?d=identicon&s=25 Peter Hickman (Guest)
on 2013-11-04 10:08
(Received via mailing list)
If you are building an XML file why are you then converting it to YAML?
Why
not write it out as is?
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-11-04 11:42
(Received via mailing list)
On Nov 4, 2013, at 2:51 AM, Rochit Sen <lists@ruby-forum.com> wrote:

> x.comment! "A test xml for test cases"
>
> OR what could be another approach to generate an xml file?
>
> Thanks
>

You are already telling builder to write to $stdout, you shouldnt even
have a File.open in there. Also, the builder object x is not YAMLizable
with that target, per se.

What you probably (?) want to be doing is more like this:

   f = File.new('dump.xml','w')
   x = Builder::XmlMarkup.new(:target => f, :indent => 1)
   ...
   f.close
0faae51834740c2142efb5af4b8d8175?d=identicon&s=25 Rochit Sen (rochitsen)
on 2013-11-05 12:28
tamouse m. wrote in post #1126383:
> On Nov 4, 2013, at 2:51 AM, Rochit Sen <lists@ruby-forum.com> wrote:
>
>> x.comment! "A test xml for test cases"
>>
>> OR what could be another approach to generate an xml file?
>>
>> Thanks
>>
>
> You are already telling builder to write to $stdout, you shouldnt even
> have a File.open in there. Also, the builder object x is not YAMLizable
> with that target, per se.
>
> What you probably (?) want to be doing is more like this:
>
>    f = File.new('dump.xml','w')
>    x = Builder::XmlMarkup.new(:target => f, :indent => 1)
>    ...
>    f.close

Thanks Tamouse. Dont know why that didnt trigger to me before. Able to
generate a xml now.

Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.