Re: Accessing name part of attributes using XML::LIbXML::Attr class

Michael Ludwig <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Ira T Taylor schrieb:
> XML::LibXML::Nodes  has a method:
> attributes
>  @attributelist = $node->attributes();
> This function returns all attributes and namespace declarations
> assigned to the given node.
>
> Because XML::LibXML does not implement namespace declarations and
> attributes the same way, it is required to test what kind of node is
> handled while accessing the functions result.

Hi Ira,

could be, but if so, I can't see how it is related to the question.

> If this function is called in array context the attribute nodes are
> returned as an array. In scalar context the function will return a
> XML::LibXML::NamedNodeMap object.
>
> I am using the XML::LibXML::Iterator class.  I can access the value
> part of an attribute using the getValue() method of the Attr class,
> but I can't figure out how to access the value part of the attribute.

I guess you mean the attribute name. ::Attr is derived from ::Node, so
it inherits its methods. The following works for me:

use strict;
use warnings;
use XML::LibXML;
my $file = shift or die 'Datei!';
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file( $file);
my $xpc = XML::LibXML::XPathContext->new( $doc);
my @elms = $xpc->findnodes( '//*');
for ( @elms ) {
    print $_->nodeName, "\n";
    my @attrs = $_->attributes;
    print "\t", $_->nodeName, "\t", $_->getValue, "\n" for @attrs;
}

Usage of XPath just a convenience. Sample input file:

<files>
    <file name="casin.flv" size="717536"/>
    <file name="cat.flv" size="725477"/>
    <file name="tren1.flv" size="5291492"/>
    <file name="True Romance.avi" size="735154176"/>
</files>

Regards,

Michael Ludwig

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.