Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

XML::XPath adding new nodes

by purge (Acolyte)
on Mar 03, 2003 at 17:39 UTC ( [id://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,
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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://240085]
Approved by Russ
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found