Here's a solution with XML::Smart:
use XML::Smart ; my $xml = XML::Smart->new(q` <nl> number list 1 number list 2 <ul> unnumbered list1 unnumbered list 2 <pl> plain list 1 plain list 2 <nl> numbered list 1 numbered list 2 </nl> </pl> </ul> </nl> ` , 'html'); my $new_xml = XML::Smart->new() ; process($xml , $new_xml) ; print $new_xml->data ; sub process { my $xml = shift ; my $new_xml = shift ; foreach my $node_i ( $xml->nodes ) { my @lines = split(/\s*\n\s*/ , $node_i) ; my $type ; if ( $node_i->key eq 'nl' ) { $type = 'numbered' ;} elsif ( $node_i->key eq 'ul' ) { $type = 'unnumbered' ;} elsif ( $node_i->key eq 'pl' ) { $type = 'plain' ;} my $set_root = 1 if $new_xml->base->null ; $new_xml->{list}{type} = $type ; $new_xml = $new_xml->{list} if $set_root ; push( @{$new_xml->{'list-item'}} , @lines) ; process($node_i , $new_xml->{listitem} ) ; } }
The output is:
<?xml version="1.0" encoding="iso-8859-1" ?> <?meta name="GENERATOR" content="XML::Smart/1.5.9 Perl/5.006001 [MSWin +32]" ?> <list type="numbered"> <list-item>number list 1</list-item> <list-item>number list 2</list-item> <listitem> <list type="unnumbered"/> <list-item>unnumbered list1</list-item> <list-item>unnumbered list 2</list-item> <listitem> <list type="plain"/> <list-item>plain list 1</list-item> <list-item>plain list 2</list-item> <listitem> <list type="numbered"/> <list-item>numbered list 1</list-item> <list-item>numbered list 2</list-item> </listitem> </listitem> </listitem> </list>
Note that your XML structure is very strange! If you can make something more normal will be better, since the idea of XML is not to delcare a tree, but to declare a document that can be read by other programs, and a crazy DTD will make this impossible in some languages. Note that if you don't want to share this type of document, maybe XML is not the best choice to store your tree.

Other crazy thing that you have is that tag <list>, where the first, the root, is used as a node with the list-item inside:

<list type="numbered"> <list-item>number list 1</list-item> <list-item>number list 2</list-item> </list>
And in the other parts you use it as a simple tag near the list-item:
<listitem> <list type="numbered"/> <list-item>numbered list 1</list-item> <list-item>numbered list 2</list-item> </listitem>
Also you have 2 different tags with similar names, <listitem> and <list-item>. Will be better to have something different, like <subitem> and <listitem>.

Also I don't understand why have <list> and <listitem> as a new level for items! So, my suggestion for you XML is:

<list type="numbered"> <item>number list 1</item> <item>number list 2</item> <list type="unnumbered"/> <item>unnumbered list 1</item> <item>unnumbered list 2</item> <list type="plain"/> <item>plain list 1</item> <item>plain list 2</item> <list type="numbered"/> <item>numbered list 1</item> <item>numbered list 2</item> </list> </list> </list> </list>
Is smaller and represent a similar tree with the same informations. So, I say again, if you can, please, change this crazy DTD!

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to Re: Text to XML by gmpassos
in thread Text to XML by murugu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.