xml parsing - Perl XML::Simple parser decoding inconsistency -


i have encountered "inconsistency" (for me) while parsing xml:

use 5.14.2; use strict; use warnings;  use xml::simple; use data::dumper;  $xml; {local $/; $xml = <data>;}  $xmlparsed = xmlin($xml,             keyattr => {phone => 'type', tankstelle => 'id'},             forcearray => [ 'phone' ],                         contentkey => '-content',                        ); dumper($$xmlparsed{'tankstelle'});   __data__ <?xml version="1.0"?> <tankstellen>     <tankstelle>         <id>63</id>         <phone type="main">0911 731586</phone>         <phone type="fax">0911 7592228</phone>         <number/>     </tankstelle>     <tankstelle>         <id>64</id>         <phone type="main">0911 732011</phone>         <phone type="fax"></phone>         <number>64</number>     </tankstelle>     <tankstelle>         <id>91</id>         <phone type="main">0911 732926</phone>         <phone type="fax">0911 732917</phone>         <number/>     </tankstelle>     <tankstelle>         <id>92</id>         <phone type="main">0911 737577</phone>         <phone type="fax"></phone>         <number/>     </tankstelle> </tankstellen> 

sometimes number hash , string. if type="fax" empty main contains content.

i tried different options parser rid of hash in main , number without luck.

'64' => {         'number' => '64',         'phone' => {                    'main' => {                              'content' => '0911 732011'                            },                    'fax' => {}                  }       }, '91' => {         'phone' => {                    'fax' => '0911 732917',                    'main' => '0911 732926'                  },         'number' => {}       } 

it sad xml::simple complicated xml module on cpan, yet beginners choose hoping easy ride. own documentation says this

the use of module in new code discouraged. other modules available provide more straightforward , consistent interfaces. in particular, xml::libxml highly recommended.

you have seen how difficult behave simplest xml, , has huge disadvantage in treats attributes same way elements.

taking author's advice, short program produces data structure think want, advantage can amend create structure xml.

use strict; use warnings;  use xml::libxml; use data::dump;  $xml = xml::libxml->load_xml(io => \*data);  %data;  $ts ($xml->findnodes('/tankstellen/tankstelle')) {    $id = $ts->findvalue('id');    $data{$id}{number} = $ts->findvalue('number');    $phone ($ts->findnodes('phone')) {     $type = $phone->findvalue('@type');     $data{$id}{phone}{$type} = $phone->findvalue('text()');   } }  dd \%data;   __data__ <?xml version="1.0"?> <tankstellen>     <tankstelle>         <id>63</id>         <phone type="main">0911 731586</phone>         <phone type="fax">0911 7592228</phone>         <number/>     </tankstelle>     <tankstelle>         <id>64</id>         <phone type="main">0911 732011</phone>         <phone type="fax"></phone>         <number>64</number>     </tankstelle>     <tankstelle>         <id>91</id>         <phone type="main">0911 732926</phone>         <phone type="fax">0911 732917</phone>         <number/>     </tankstelle>     <tankstelle>         <id>92</id>         <phone type="main">0911 737577</phone>         <phone type="fax"></phone>         <number/>     </tankstelle> </tankstellen> 

output

{   63 => {           number => "",           phone  => { fax => "0911 7592228", main => "0911 731586" },         },   64 => {           number => 64,           phone => { fax => "", main => "0911 732011" }         },   91 => {           number => "",           phone  => { fax => "0911 732917", main => "0911 732926" },         },   92 => {           number => "",           phone => { fax => "", main => "0911 737577" }         }, } 

tool completed successfully


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -