Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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,
Simon.

Comment on XML::XPath adding new nodes
Select or Download Code
(jeffa) Re: XML::XPath adding new nodes
by jeffa (Chancellor) 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
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? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-12-25 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls