Raghu.S has asked for the wisdom of the Perl Monks concerning the following question:

Here'the code that I'm using:
#!/usr/bin/perl use XML::Simple qw/XMLin/; use XML::Simple; use Data::Dumper qw/Dumper/; use File::Basename qw/dirname basename fileparse/; use IPC::Open2 qw/open2/; use Getopt::Long qw/GetOptions/; my $config = XMLin('./appsimage_EBSB4.xml'); print Dumper($config);
--------------
cat ./appsimage_EBSB4.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE clone[ <!ENTITY envPageConfig SYSTEM "./envPageConfig.xml"> ]> <clone-config> "\&envPageConfig"; </clone-config> -------------------- cat ./envPageConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE clone[ <!ENTITY envPageConfig SYSTEM "./envPageConfig.xml"> ]> <clone-config> "&envPageConfig"; </clone-config> ohpr434:EBSB11:/home/oracle/test$cat envPageConfig.xml <comt4> <bolinfpwd>welcome</bolinfpwd> </comt4>
----------------

When I execute test.pl

$VAR1 = { 'comt4' => [ { 'bolinfpwd' => 'welcome' }, { 'bolinfpwd' => 'welcome' }, { 'bolinfpwd' => 'welcome' } ] };
-----------------------

Can you help me limiting this array to single value which is the ideal case.

Replies are listed 'Best First'.
Re: Duplicate entries with nested xml
by tobyink (Canon) on Sep 04, 2014 at 09:22 UTC

    The documentation for XML::Simple says:

    The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.

    The major problems with this module are the large number of options and the arbitrary ways in which these options interact - often with unexpected results.

    It might as well say:

    Abandon all hope, ye who enter here.

    If you want stable, reliable XML handling, XML::Simple is really not your friend.

    Once the formatting of your question is fixed so I can actually see your XML, I may be able to offer some more specific advice.

    Update: the formatting has been fixed, but there still seems to be errors in the listing for your XML. For example, the following is not a valid XML entity reference:

    "\&envPageConfig";

    And you show two contradictory listings for the file called envPageConfig.xml.

Re: Duplicate entries with nested xml
by karlgoethebier (Abbot) on Sep 04, 2014 at 13:35 UTC

    Try this:

    use strict; use warnings; use XML::LibXML; my $file = qq(appsimage_EBSB4.xml); open my $fh, '<', $file; binmode $fh; my $doc = XML::LibXML->load_xml(IO => $fh); close $fh; print $doc->toString(); __END__ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE clone [ <!ENTITY envPageConfig SYSTEM "./envPageConfig.xml"> ]> <clone-config> <comt4> <bolinfpwd>welcome</bolinfpwd> </comt4> </clone-config>

    Edit: forgot my $doc :-(

    Please note: your entity should be written as &envPageConfig;, not "&envPageConfig";.

    Update:

    This might be helpful too:

    use strict; use warnings; use XML::Twig; use Data::Dumper; my $file = qq(appsimage_EBSB4.xml); my $hash_ref = XML::Twig->new()->parsefile( $file )->simplify(); print Dumper($hash_ref); print $hash_ref->{comt4}->{bolinfpwd};

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      This part:

      my $file = qq(appsimage_EBSB4.xml); open my $fh, '<', $file; binmode $fh; my $doc = XML::LibXML->load_xml(IO => $fh); close $fh;

      Could be written more succinctly as:

      my $doc = XML::LibXML->load_xml( location => qq(appsimage_EBSB4.xml) +);
        "...written more succinctly..."

        Sure tobyink, nothing but the truth. I didn't know this.

        To be honest: I'm still using XML::Simple :-[ because i got a lot of config stuff written with it and cheated my example from the docs of XML::LibXML.

        N.B.: I also tried XML::LibXML::Simple with the stuff the OP provided, but it failed with the entity...

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»