Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I use to generate a local XML file?

I have a project that I am working on and I do not know much about Rails or Ruby.

I need to generate an XML file from user input. Can some direct me to any resource that can show me how to do this pretty quickly and easily?

like image 798
ironmantis7x Avatar asked Jun 25 '13 21:06

ironmantis7x


People also ask

What program creates XML files?

Microsoft XML Notepad is an application that allows you to create and edit XML documents quickly and easily. With this tool, the structure of your XML data is displayed graphically in a tree structure. The interface presents two panes: one for the structure, and one for the values.

How do I create an XML file from Excel?

Click File > Save As, and select the location where you want to save the file. , point to the arrow next to Save As, and then click Other Formats. In the File name box, type a name for the XML data file. In the Save as type list, click XML Data, and click Save.

How do I create an empty XML file?

You can create a blank XML document from scratch. This document only contains a single root element ( <ROOT> ). Open the New XML dialog (File > New > XML). In the Other XML tab, select Empty XML and click OK.


2 Answers

The Nokogiri gem has a nice interface for creating XML from scratch. It's powerful while still easy to use. It's my preference:

require 'nokogiri'
builder = Nokogiri::XML::Builder.new do |xml|
  xml.root {
    xml.products {
      xml.widget {
        xml.id_ "10"
        xml.name "Awesome widget"
      }
    }
  }
end
puts builder.to_xml

Will output:

<?xml version="1.0"?>
<root>
  <products>
    <widget>
      <id>10</id>
      <name>Awesome widget</name>
    </widget>
  </products>
</root>

Also, Ox does this too. Here's a sample from the documenation:

require 'ox'

doc = Ox::Document.new(:version => '1.0')

top = Ox::Element.new('top')
top[:name] = 'sample'
doc << top

mid = Ox::Element.new('middle')
mid[:name] = 'second'
top << mid

bot = Ox::Element.new('bottom')
bot[:name] = 'third'
mid << bot

xml = Ox.dump(doc)

# xml =
# <top name="sample">
#   <middle name="second">
#     <bottom name="third"/>
#   </middle>
# </top>
like image 125
the Tin Man Avatar answered Oct 12 '22 15:10

the Tin Man


Nokogiri is a wrapper around libxml2.

Gemfile gem 'nokogiri' To generate xml simple use the Nokogiri XML Builder like this

xml = Nokogiri::XML::Builder.new { |xml| 
    xml.body do
        xml.node1 "some string"
        xml.node2 123
        xml.node3 do
            xml.node3_1 "another string"
        end
        xml.node4 "with attributes", :attribute => "some attribute"
        xml.selfclosing
    end
}.to_xml

The result will look like

<?xml version="1.0"?>
<body>
  <node1>some string</node1>
  <node2>123</node2>
  <node3>
    <node3_1>another string</node3_1>
  </node3>
  <node4 attribute="some attribute">with attributes</node4>
  <selfclosing/>
</body>

Source: http://www.jakobbeyer.de/xml-with-nokogiri

like image 36
David Okwii Avatar answered Oct 12 '22 14:10

David Okwii