Arcane has asked for the wisdom of the Perl Monks concerning the following question:

hello, I am trying to generate an XML document in the following form:
<ROOT> <Child1> <SubChild1>xxx</SubChild1> <SubChild2>xxx</SubChild2> <SubChild3>xxx</SubChild3> </Child1> <Child2> <SubChild1>xxx</SubChild1> <SubChild2>xxx</SubChild2> <SubChild3>xxx</SubChild3> <SubChild4>xxx</SubChild4> <SubChild> <SubSubChild1>xxx</SubSubChild1> <SubSubChild2>xxx</SubSubChild2> <SubSubChild3>xxx</SubSubChild3> <SubSubChild4>xxx</SubSubChild4> <SubSubChild5>xxx</SubSubChild5> <SubSubChild6>xxx</SubSubChild6> </SubChild> <SubChild5>xxx</SubChild5> </Child2> </ROOT>
I have done it just by reading my data in from a csv file, but I was wondering if there is a perl module that could help me do this. Also, the <Child2> section can be repeated depending on my data that I have passed in. So it could look like:
<ROOT> <Child1> <SubChild1>xxx</SubChild1> <SubChild2>xxx</SubChild2> <SubChild3>xxx</SubChild3> </Child1> <Child2> <SubChild1>xxx</SubChild1> <SubChild2>xxx</SubChild2> <SubChild3>xxx</SubChild3> <SubChild4>xxx</SubChild4> <SubChild> <SubSubChild1>xxx</SubSubChild1> <SubSubChild2>xxx</SubSubChild2> <SubSubChild3>xxx</SubSubChild3> <SubSubChild4>xxx</SubSubChild4> <SubSubChild5>xxx</SubSubChild5> <SubSubChild6>xxx</SubSubChild6> </SubChild> <SubChild5>xxx</SubChild5> </Child2> <Child3> <SubChild1>xxx</SubChild1> <SubChild2>xxx</SubChild2> <SubChild3>xxx</SubChild3> <SubChild4>xxx</SubChild4> <SubChild> <SubSubChild1>xxx</SubSubChild1> <SubSubChild2>xxx</SubSubChild2> <SubSubChild3>xxx</SubSubChild3> <SubSubChild4>xxx</SubSubChild4> <SubSubChild5>xxx</SubSubChild5> <SubSubChild6>xxx</SubSubChild6> </SubChild> <SubChild5>xxx</SubChild5> </Child3> </ROOT>
any help, or an example on how to use a perl module to do this would be great. I have been playing around with XML::DOM, but am not having lots of luck. thanks again

Replies are listed 'Best First'.
Re: Generating an XML Document
by atcroft (Abbot) on Nov 18, 2005 at 18:11 UTC

    I would recommend you take a look at Perl and XML by Erik T. Ray and Jason McIntosh (O'Reilly, 2002). The sample chapter currently provided (Chpt. 3) covers the basics of reading and writing XML (with the examples of writing XML in that chapter using XML::Writer).

    HTH.

Re: Generating an XML Document
by philcrow (Priest) on Nov 18, 2005 at 19:24 UTC
    I like to generate XML with the template toolkit (Template) so that the xml is not litter in my code. The toolkit has its own little language with statements like FOREACH for iterating and IF for conditionals (it's a bit like Perl, but much simpler).

    Sometimes I use Inline::TT which allows me to keep the template in the script. <disclosure>I'm the current maintainer of Inline::TT</disclosure>

    Phil