You can use XML::DOM or XML::LibXML as well as others, but first you'll have
to start with well-formed XML document (which your example is not). I prefer
XML::LibXML.

Say you have the following:

<doc>
  <item name="first">
    <subheader> test </subheader>
  </item>
  <item name="third">
    <subheader> test </subheader>
  </item>
</doc>

Then you can do this with XML::LibXML:

# Parse xml string (you can also use parse_file)
my $doc = XML::LibXML->new->parse_string($xml);
my $items = $doc->documentElement;

# Create your new element
my $secondItem = XML::LibXML::Element->new('item');
$secondItem->setAttribute( name => 'second');
$secondItem->appendTextChild( subheader => ' test ');

# Put the element into the proper place under items
my ($thirdItem) = $items->findnodes('//[EMAIL PROTECTED]"third"]');
$items->insertBefore($secondItem, $thirdItem);

# Print the results
print $doc->toString;


> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Mohammed Gazal
> Sent: Thursday, January 20, 2005 9:24 PM
> To: perl-unix-users@listserv.ActiveState.com
> Cc: perl-win32-users@listserv.ActiveState.com
> Subject: how to add a new item to a xml
> 
> 
> Hello ,
> 
>  I have a xml doc like this 
> 
>  <item name ="first">
>       <subheader> test </subheader>
>  </item>
> <item name = "third">
>    <subheader> test </subheader>
> </item> 
> 
> now if i want to insert a new item for ex: second inbetween 
> the first and third item elements which xml modules help in 
> doing this? and do you have any examples? 
> 
>  Any help will be appreciated.
> Thxs,
> 
>  Gazal. 
> 
> _______________________________________________
> Perl-Win32-Users mailing list 
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to