Creating '.plist' (XML) file in ruby 2.1

Hello,

I'd like to create the following '.plist' (XML) file[1] from within a ruby program:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>org.atma.local</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/atma/Code/scripts/local.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>3600</integer>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

I would like to have variables for 'integer' (e.g. <integer><%= var %></interger> or something). What is the best way to create a predefined file? I've code around using a strange notation '<<' when it comes to dumping lines of text or XML into ruby scripts but I can't find any specific examples right now.

best regards,

[1] https://gist.github.com/atmosx/10936292

Panagiotis (atmosx) Atmatzidis

email: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

"As you set out for Ithaca, hope the voyage is a long one, full of adventure, full of discovery [...]" - C. P. Cavafy

Quoting Panagiotis Atmatzidis (atma@convalesco.org):

I'd like to create the following '.plist' (XML) file[1] from within a ruby program:

The XML library that's included in MRI (REXML) allows you to write XML
programmatically. Sadly, the documentation site indicated in the RI
page (www.germane-software.com) is currently not reachable, but some
docs are here:

http://ruby-doc.org/stdlib-2.1.1/libdoc/rexml/rdoc/index.html

If you are just interested in XML code that's correctly parsed
(i.e. you do not care about the resulting indentation), you can start
like this:

--8<----8<----8<----8<----8<--
require 'rexml/rexml'
require 'rexml/document'

d=REXML::Document::new

n=REXML::Element::new('plist')
n.add_attribute('version','1.0')
d.push(n)

n2=REXML::Element::new('dict')
n.push(n2)

n3=REXML::Element::new('key')
n3.add_text('Label')
n2.push(n3)

···

Subject: Creating '.plist' (XML) file in ruby 2.1
  Date: Wed 16 Apr 14 11:53:28PM +0200

#
# go on inserting elements
#

d.write
--8<----8<----8<----8<----8<--

The above example gives:

<plist version='1.0'><dict><key>Label</key></dict></plist>

There is some useful info in

ri REXML::Document

and

ri REXML::Element

Carlo

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Henry

···

On 17/04/2014, at 9:53 am, Panagiotis Atmatzidis <atma@convalesco.org> wrote:

Hello,

I'd like to create the following '.plist' (XML) file[1] from within a ruby program:

I use builder: GitHub - jimweirich/builder: Provide a simple way to create XML markup and data structures.

···

On Thu, Apr 17, 2014 at 12:35 AM, Carlo E. Prelz <fluido@fluido.as> wrote:

        Subject: Creating '.plist' (XML) file in ruby 2.1
        Date: Wed 16 Apr 14 11:53:28PM +0200

Quoting Panagiotis Atmatzidis (atma@convalesco.org):

I'd like to create the following '.plist' (XML) file[1] from within a ruby program:

Hello,

thank you all for the replies

       Subject: Creating '.plist' (XML) file in ruby 2.1
       Date: Wed 16 Apr 14 11:53:28PM +0200

Quoting Panagiotis Atmatzidis (atma@convalesco.org):

I'd like to create the following '.plist' (XML) file[1] from within a ruby program:

I use builder: GitHub - jimweirich/builder: Provide a simple way to create XML markup and data structures.

this solution seems to be the easier thanks!

Panagiotis (atmosx) Atmatzidis

email: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

"As you set out for Ithaca, hope the voyage is a long one, full of adventure, full of discovery [...]" - C. P. Cavafy

···

On 18 Απρ 2014, at 03:01 , tamouse pontiki <tamouse.lists@gmail.com> wrote:

On Thu, Apr 17, 2014 at 12:35 AM, Carlo E. Prelz <fluido@fluido.as> wrote: