Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

XML::XPath adding new nodes

by purge (Acolyte)
on Mar 03, 2003 at 17:39 UTC ( #240085=perlquestion: print w/replies, xml ) Need Help??
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,

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. :)


    (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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://240085]
Approved by Russ
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-01-22 07:28 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (232 votes). Check out past polls.