ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
Re: Accessing name part of attributes using XML::LIbXML::Attr class
by Michael Ludwig other posts by this author
Jun 10 2009 7:33AM messages near this date
view in the new Beta List Site
Accessing name part of attributes using XML::LIbXML::Attr class | Parsing emoty fields XML using perl
& XSLT 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
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Ira T Taylor
Michael Ludwig

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved