Showing us a sample of the code you have tried, or at least telling us what modules you are using, would help a great deal in providing a good answer.

A light weight way to perform your trick if you are not fussy about the order of the elements in the result is:

use strict; use XML::Simple; my $org = <<'XML'; <FILER url='http://somewhere'> <PROJECT name='test_project'/> <USER email='some@email.com'/> </FILER> XML # parse the input string my $root = XMLin ($org, KeepRoot => 1, ); # Insert the element $root->{FILER}{LOCATION}{name} = '1stFloor'; # Dump the result print XMLout ($root, RootName => undef);

which prints:

<FILER url="http://somewhere"> <LOCATION name="1stFloor" /> <PROJECT name="test_project" /> <USER email="some@email.com" /> </FILER>

If you need to retain the original element order then you need something like XML::TreeBuilder:

use strict; use XML::TreeBuilder; my $org = <<'XML'; <FILER url='http://somewhere'><PROJECT name='test_project'/><USER emai +l='some@email.com'/></FILER> XML # parse the input string my $root = XML::TreeBuilder->new (); $root->parse ($org); # Insert the element my $ins = XML::Element->new ('LOCATION', name => '1stFloor'); $root->push_content ($ins); # Dump the result print $root->as_XML ();

which prints:

<FILER url="http://somewhere"><PROJECT name="test_project"></PROJECT>< +USER email="some@email.com"></USER><LOCATION name="1stFloor"></LOCATI +ON></FILER>

or XML::Twig:

use strict; use XML::Twig; my $org = <<'XML'; <FILER url='http://somewhere'><PROJECT name='test_project'/><USER emai +l='some@email.com'/></FILER> XML # parse the input string my $root = XML::Twig->new (); $root->parse ($org); # Insert a new element my $ins = XML::Twig::Elt->new ('LOCATION', {name => '1stFloor'}); $ins->paste ('last_child', $root->root ()); # Dump the result $root->print ();

which prints:

<FILER url="http://somewhere"><PROJECT name="test_project"/><USER emai +l="some@email.com"/><LOCATION name="1stFloor"/></FILER>

DWIM is Perl's answer to Gödel

In reply to Re: XML handling by GrandFather
in thread XML handling by Anonymous Monk

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.