On Mon, 2007-08-27 at 10:28 +0530, Karthick Radhakrishnan
wrote:
> Bjoern & Jonathan,
> Thanks for your reply. Here is the detailed one.
> There is no module XML::LibXML::Element. Instead of
package I said
> module. sorry for that.
>
> I'm trying to insert a new tag. See below xml
>
> The Error I got is
> "
> Can't locate object method "appendChild" via
package
> "XML::LibXML::NodeList" at xmlCreate.pl line
34.
> "
>
> Code
> use strict;
> use warnings;
> use XML::LibXML;
>
> my $xmlFile = "library.xml";
> my $isbn = "076455106X";
>
> my $xmlObj = XML::LibXML->new();
> my $doc = $xmlObj->parse_file($xmlFile);
> my $book = $doc->findnodes("//book[isbn =
'$isbn']");
>
In a scalar context (such as you have above) findnodes()
returns an
XML::LibXML::NodeList object - irrespective of the number
of nodes
found. If you are absolutely sure that you will only find
one node with
your expression then you can do:
my $book = ($doc->findnodes("//book[isbn =
'$isbn']"))[0];
(i.e putting the RHS in a list context and taking the first
element of
the list or:
my $book = $doc->findnodes("//book[isbn =
'$isbn']")->shift();
(i.e. shift() the first element off the
XML::LibXML::NodeList )
/J
--
Winners don't do de-tox
_______________________________________________
Perl-XML mailing list
Perl-XML listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|