in reply to Extract attributes/values from XML using perl / declaring wires in verilog using perl

The XML is not well formed. In the Fourth element, B and C are missing the closing double quotes, and Root's closing tag needs a slash. Your specification conflicts with the output: why are AA, BB, CC, and DD not printed?

In xsh, you'd just hash the attributes:

open file.xml ; $h := hash . //@* ; echo { join "\n", keys $h } ;

Similarly, in XML::LibXML:

#! /usr/bin/perl use warnings; use strict; use XML::LibXML; my $xml = 'XML::LibXML'->load_xml( location => 'file.xml' ); my %h; for ($xml->findnodes('//@*')) { $h{ $_->value } = 1; } print "$_\n" for keys %h;

If you only want attributes of elements with no subelements, change the XPath expression to

//*[not(./*)]/@*

Update: Not so simple in XML::Simple:

#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::Simple; my $xml = XMLin('file.xml', ForceArray => 1, KeyAttr => [], ForceContent => 1); my %h; @h{ map values %{ $_->[0] }, grep 'ARRAY' eq ref, map values %{ $_->[0] }, values %{ $xml->{tags}[0] } } = (); say for keys %h;
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Extract attributes/values from XML using perl
by gr.d (Novice) on Jan 08, 2016 at 08:06 UTC

    a minor update on my post, any thoughts on how to do this?

    thanks!

      a minor update on my post, any thoughts on how to do this?

      :) its minor, you handle it using provided code as example

        thanks :)

Re^2: Extract attributes/values from XML using perl
by gr.d (Novice) on Jan 07, 2016 at 13:14 UTC

    the AA BB CC are just examples, not really concentrating on that now

Re^2: Extract attributes/values from XML using perl
by gr.d (Novice) on Jan 08, 2016 at 05:08 UTC

    I'm currently using the perl version 5.8.8 and "feature" is in perl 5.10 if I'm not wrong. Anyway to surpass this hurdle?. Since the upgrade is in the admins hand and that is not happening anytime soon, was hoping something can be done

      It's not relevant to the problem. Use print instead of say and add a newline.
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Agree with choroba's post that you don't need the feature 'say' to solve your issue. But FYI, in Perl you can create your own global functions with the core module Exporter. A very naive implementation:

      In MyFeatures.pm:

      package MyFeatures; use strict; use warnings; use Exporter qw/ import /; our @EXPORT_OK = qw/ say /; sub say { my $str = shift; print "$str\n"; } 1; # return true

      In your program:

      #!/usr/bin/perl use strict; use warnings; use MyFeatures qw/ say /; my $str = 'Hello, world.'; say $str; say q{Nice weather, isn't it?}; __END__

      Output:

      Hello, world. Nice weather, isn't it?

      Hope this helps.

      The way forward always starts with a minimal test.

        That's useful, Thank you

Re^2: Extract attributes/values from XML using perl
by gr.d (Novice) on Jan 07, 2016 at 13:12 UTC

    Corrected

    how can this be done with XML::Simple?