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

Is there a function in XML::XPath to print to file?
use XML::XPath; my $xp = XML::XPath->new(); my $xml_pi = XML::XPath::Node::PI->new('xml', 'version="1.0"'); my $root = XML::XPath::Node::Element->new('root'); my $node = XML::XPath::Node::Element->new('node'); $root->appendChild($node); # $root->printToFileFunction("filename.xml");

Replies are listed 'Best First'.
(jeffa) Re: XML::XPath printToFile function?
by jeffa (Bishop) on Mar 19, 2002 at 05:26 UTC
    According to the docs for XML::XPath::Node::Element, there is no such method. You could do it yourself, however:
    open (FH,'>filename.xml') or die $!; print FH $root->toString; close FH;

    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)
    
Re: XML::XPath printToFile function?
by Matts (Deacon) on Mar 19, 2002 at 10:22 UTC
    As jeffa said, there is no such method.

    Since everything is already in memory anyway with XML::XPath, I decided not to bother with a direct-to-filehandle output method. If you need that, try XML::LibXML, which does have that.

Re: XML::XPath printToFile function?
by mdillon (Priest) on Mar 19, 2002 at 05:41 UTC
    XML::XPath is not meant for building in-memory XML documents from scratch. You should probably have a look at either XML::Writer or XML::Generator. The POD documentation for either of those modules should give you adequate documentation for using them.
Re: XML::XPath printToFile function?
by Anonymous Monk on Mar 19, 2002 at 16:30 UTC
    Thanks for the replies. I'm sure this would be better as a new post but the short of what I am trying to do is this. Add elements to nodes based on the attribute value or create a new file if it does not exist. The basic file would be this.
    <root> <node id = "1"> <request> <question> ... </question> </request> </node> </root>
    Answers would be appended to the node with a matching id.
    <root> <node id = "1"> <request> <question> ... </question> </request> <reply> <answer> ... </answer> </reply> <reply> <answer> ... </answer> <reply> </node> </root>
    I would also like to remove nodes based on the id attribute. And be able to do all this through a Jabber bot similar to Peter Saint-Andre's surveyor.