Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Trying to use XML::Smart: How do I get the attribute names of an XML element using XML::Smart?

by jira0004 (Monk)
on Nov 02, 2005 at 21:50 UTC ( #505123=perlquestion: print w/ replies, xml ) Need Help??
jira0004 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

There are a variety of Perl packages for parsing and writing XML (See CPAN).

I am trying to use XML::Smart to parse and work with XML.

Here is what my source code is like right now:

    $xml_element_obj = new XML::Smart( $xml_element_text, 'XML::Parser' );

Where $xml_element_text contains the XML element being parsed. Creating the new $xml_element_obj as indicated above causes the XML text in $xml_element_text to be parsed.

What I want to do next is I want to get the names of the XML element attributes out of $xml_element_obj. But, I can't figure out how to get $xml_element_obj to give me back the attribute names. I've looked at the XML::Smart documentation, but I haven't figured out yet how to use this Perl module to get the XML attribute names out of the XML element.

Does any one know how to get the names of an XML element's attributes out of an XML::Smart instance?

Please advise.

Regards, Peter Jirak

jira0004@yahoo.com

Comment on Trying to use XML::Smart: How do I get the attribute names of an XML element using XML::Smart?
Re: Trying to use XML::Smart: How do I get the attribute names of an XML element using XML::Smart?
by ickyb0d (Monk) on Nov 02, 2005 at 22:10 UTC

    I don't really have any experience using the module... but...

    Based on looking at the documentation

    You can get the list of values of an attribute looking in all multiple nodes:

    ## Get all the server types: my @types = $XML->{server}('[@]','type') ;

    Further down it cites

    my @names = $this->{method}{wxFrame}{arg}{name}('<@') ; #### @names = (parent , id , title) ;

    It also gives an example

    my $name = $XML->{server}{name}->content ;

    hope this helps.

Re: Trying to use XML::Smart: How do I get the attribute names of an XML element using XML::Smart?
by antirice (Priest) on Nov 02, 2005 at 22:41 UTC

    Well, you could use args().

    i.e.
    #!/usr/bin/perl -l use XML::Smart; my $x = XML::Smart->new(<<'XML'); <?xml version="1.0" encoding="iso-8859-1"?> <hosts> <server os="linux" type="redhat" version="8.0"> <address>192.168.0.1</address> <address>192.168.0.2</address> </server> <server os="linux" type="suse" version="7.0"> <address>192.168.1.10</address> <address>192.168.1.20</address> </server> <server address="192.168.2.100" os="linux" type="conectiva" versio +n="9.0"/> </hosts> XML $,=$"; print $_->key . ":" , $_->args for $x->cut_root->nodes; __END__ server: os type version server: os type version server: address os type version

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://505123]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-08-27 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (252 votes), past polls