On Wed, 3 Oct 2007, Sunil wrote:
> Below is example code snippet which extracts NAME node
value. I want to
> extract MESSAGE node Type attribute value.
>
> XML FILE:
>
> <MESSAGE Type="test">
> <NAME>extract attribute value<NAME>
> </MESSAGE>
>
> CODE SNIPPET:
>
> foreach my $node (
$xmlFH->find("MESSAGE")->get_nodelist ) {
>
>
> my $name =
$node->find("NAME")->string_value;
>
> }
>
>
> Basically, I need to extract attribute value in a node
using XPath. How
> should I do that?
I'd be looking at something like
$xmlFH->findvalue('//MESSAGE/ Type')
However, I recommend that you delve into XPath enough to
be able to
resolve these kinds of problems. Here are some tips:
1. Use Data: umper to
print out the results of anything. Often you'll
just get an object reference, but it'll tell you what type
of object
you're dealing with (ie. XML::LibXML::Attr vs.
XML::LibXML::Element).
2. If you print the results of the serialize function, you
can see what's
in your node. So, try something like this:
------------------------------------------------------------
-------------------
#!/usr/bin/perl
use Data: umper;
use Scalar::Util;
.......
$node = $xmlFH->find('whatever');
shownode($node);
sub shownode {
my($node) = _;
print(('-' x 40) . "n");
print Dumper $node;
if(blessed($node) and $node->can('serialize')) {
print $node->serialize();
} else {
print "Can't serialize this!n";
}
print(('-' x 40) . "n");
}
------------------------------------------------------------
-------------------
That should point you towards any difficulties you
encounter.
------------------------------------------------------------
---------
| Name: Tim Nelson | Because the Creator is,
|
| E-mail: wayland smartchat.net.au | I am
|
------------------------------------------------------------
---------
----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+++ s+: a- C++$ U+++$ P+++$ L+++ E- W+ N+ w--- V-
PE(+) Y+>++ PGP->+++ R(+) !tv b++ DI++++ D G+
e++>++++ h! y-
-----END GEEK CODE BLOCK-----
_______________________________________________
Perl-XML mailing list
Perl-XML listserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
|