Generating an XML file

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 methodname’ for #<IO:> (NoMethodError)

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

Thanks

If you are building an XML file why are you then converting it to YAML?
Why
not write it out as is?

On Nov 4, 2013, at 2:51 AM, Rochit S. [email protected] 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

tamouse m. wrote in post #1126383:

On Nov 4, 2013, at 2:51 AM, Rochit S. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs