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

Ok.. I've been trying far too long to work this out too long now, taking the following example:

<form> <page id="0"> <field name="alpha">123</field> <field name="beta">234</field> </page> <page id="1"> </page> </form>
Now I can get out all the information fine no problem, but I want to add a new 'field' inside 'page' i.e:

<code> <form> <page id="0"> <field name="alpha">123</field> <field name="beta">234</field> <field name="gamma">345</field> </page> <page id="1"> </page> </form>
now how do I do this with XML::XPath? using setNodeText, what XPath do I use?
I've tried several combinations to no avail. Help me please its driving me nuts!

Thankyou humbly,
Simon.

Replies are listed 'Best First'.
(jeffa) Re: XML::XPath adding new nodes
by jeffa (Bishop) on Mar 04, 2003 at 13:57 UTC
    I don't think you are using the right tool here. XML::XPath is most useful for finding things, not necessarily adding things. One solution is to pair XML::Parser and XML::Writer, you can distill more of this solution over at XML Search and Replace. Another thought is to use XML::Twig (i am starting to think of this module as the Swiss-Army chainsaw of XML processing). Yet another solution is to use XML Shell. This will be most helpful for when you need to edit complex XML for one-time, on the fly tasks. Good luck. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks Jeffa,

      I've dumped XML::XPath in this case anyway for XML::LibXML which is both faster and seems more logical. Its not for everybody mind as its based on libxml2, but suits my server requirements fine.
      Thanks for the link, it looks like it could be helpful.

      Simon.
        I took a look at XML::Twig, played with it a bit and voila.
        XML::Twig really is the dogs...