Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How do I get a list in a perl hash generated from an XML?

by choroba (Archbishop)
on Apr 19, 2021 at 09:53 UTC ( #11131464=note: print w/replies, xml ) Need Help??


in reply to How do I get a list in a perl hash generated from an XML?

Here's how to get the requested structure using XML::LibXML.
#! /usr/bin/perl use warnings; use strict; use XML::LibXML; my $dom = 'XML::LibXML'->load_xml(location => shift); my %struct; for my $ref ($dom->findnodes('/root/ref')) { for my $child ($ref->findnodes('*')) { push @{ $struct{ $ref->{name} }{ $child->nodeName } }, { map { $_->nodeName => $_->value } $child->findnodes('@* +') }; } } use Data::Dumper; print Dumper \%struct;

But converting from XML::Simple to XML::LibXML usually changes the code much more. You can extract only the information you need from the XML, so you don't need to build the whole structure at all.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: How do I get a list in a perl hash generated from an XML?
by PV66 (Novice) on Apr 19, 2021 at 12:19 UTC
    This does make sense @choroba. Out of curiosity, what if my XML had more tags, for example if it looked like:
    <root> <config> <build host="https://cpzdomain.local"/> </config> <ref name="abc_sia_%version1.ref%"> <func envname = "test01" objectdir = "/home/pv66" base="default_771"/> </ref> </root>
    And I wanted to just make a hash as in my question for only the elements in the <ref> tag, is there anyway to do it with the XML::Simple module?
      I can't help you with XML::Simple (and I don't want to). If you need advice to migrate the whole logic to XML::LibXML , though, I can be your consultant.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2021-05-13 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (144 votes). Check out past polls.

    Notices?