<?xml version="1.0" encoding="windows-1252"?>
<node id="1008508" title="Re: Creating nested elements in XML::Smart" created="2012-12-12 09:34:29" updated="2012-12-12 09:34:29">
<type id="11">
note</type>
<author id="105128">
Jenda</author>
<data>
<field name="doctext">
&lt;code&gt;
use XML::Rules;

my $parser = XML::Rules-&gt;new(rules =&gt; {}, ident =&gt; '  ');

print $parser-&gt;toXML(
Shelf_Info =&gt; {
	switch =&gt; [
		{Name =&gt; "Puebla01", shelfType =&gt; [7480], commentText =&gt; ['ATM Core switch']},
		{Name =&gt; "Puebla02", shelfType =&gt; [7440], commentText =&gt; ['ATM access switch']},
	]
});
&lt;/code&gt;

&lt;p&gt;Not XML::Smart though. It makes use of the fact that the value of a tag attribute may only be a single string, not a hash or array so it's clear that the shelfType and commentText must be a child tag, not an attribute. Generally XML::Rules attempts to create the simplest possible XML that'd contain the data and attribute is considered simpler than child tag.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; With version 1.16 I just uploaded to CPAN, it'd be even simpler:&lt;/p&gt;

&lt;code&gt;
use XML::Rules ToXML =&gt; { ident =&gt; '   ' };

print ToXML(
Shelf_Info =&gt; {
	switch =&gt; [
		{Name =&gt; "Puebla01", shelfType =&gt; [7480], commentText =&gt; ['ATM Core switch']},
		{Name =&gt; "Puebla02", shelfType =&gt; [7440], commentText =&gt; ['ATM access switch']},
	]
});
&lt;/code&gt;

&lt;p&gt;You may now instantiate a parser and import a subroutine that'll call the specified method on that instance to simplify the usage.&lt;/p&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-105128"&gt;
&lt;p&gt;Jenda&lt;br&gt;
&lt;b&gt;Enoch was right!&lt;/b&gt;&lt;br/&gt;
Enjoy the last years of Rome.
&lt;/p&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1008430</field>
<field name="parent_node">
1008430</field>
</data>
</node>
