Hi, I try to get the informations contained in an xml file.

The xml file :
<?xml version='1.0'?> <Targets> <main_target> <main_target_name>Instance</main_target_name> <target> <target_name>estspc_netlist</target_name> <target_options>CELLS</target_options> </target> <target> <target_name>ibitmap</target_name> <target_options>CELLS</target_options> <target_options>STACK_MODE</target_options> </target> <target> <target_name>liberty</target_name> <target_options>STACK_MODE</target_options> <target_options>VT_MODE</target_options> <sub_target> <sub_target_name>nldm_liberty</sub_target_name> </sub_target> <sub_target> <sub_target_name>ccs_liberty</sub_target_name> <sub_target_options>CELLS</sub_target_options> <sub_target_options>VT_MODE</sub_target_options> </sub_target> </target> </main_target> <main_target> <main_target_name>Validation</main_target_name> <target> <target_name>bmval</target_name> <target_options>VT_MODE</target_options> <sub_target> <sub_target_name>bmval_lvs</sub_target_name> <sub_target_options>VT_MODE</sub_target_options> </sub_target> <sub_target> <sub_target_name>bmval_sim</sub_target_name> <sub_target_options>STACK_MODE</sub_target_options> <sub_target_options>VT_MODE</sub_target_options> </sub_target> </target> </main_target> </Targets>
My code :
#!/usr/bin/env perl # use module use XML::Simple; use Data::Dumper; # create object $xml = new XML::Simple; # read XML file $xmlData = $xml->XMLin("./xuo.xml", forcearray => 1); # print output # print Dumper($data); my $main_target = '' ; my $target = '' ; my $sub_target = '' ; my $main_target_name = '' ; my $target_name = '' ; foreach $main_target (@{$xmlData->{main_target}}) { $main_target_name = @{$main_target->{main_target_name}}[0] ; print "Main Target Name : ", $main_target_name, "\n" ; foreach $target (@{$main_target->{target}}) { $target_name = @{$target->{target_name}}[0] ; print " Target Name : ", $target_name, "\n"; # if (exists $main_target->{target}->{sub_target}) # { foreach $sub_target (@{$main_target->{target}->{sub_target +}}) { print " Sub Target Name : ", $sub_target, "\n"; } # } } } exit ;
If I comment out the lines foreach $sub_target...
then I get something correct :

Main Target Name : Instance
Target Name : estspc_netlist
Target Name : ibitmap
Target Name : liberty
Main Target Name : Validation
Target Name : bmval

But the "sub_target" is not always defined in the "target" fields of the xml file. This is the reason why it fails. But I don't know how to test the existence of this "sub_target" field to print only when this field do exist.

Do you know how to do ?

Regards.

Xuo.

In reply to I get "not a HASH reference" when parsing an XML file by xuo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.