Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


I am attempting to parse valid XML data as a string and output the result as a complex data structure (a hash of hashes and arrays) which I can then use to reference individual data elements.

I have written a small function to perform this task, which almost works. It runs without error and it does return a hash that I can reference.

The problem is that a few of the hash keys have been truncated. That is, they are missing the last character. The majority of keys, however appear fine.

I am using valid XML and the problem appears to be intermittent, although the fields affected are always the same (company_name, fee_lines, address_2, etc.)

Here is the code i'm using:

#!/usr/bin/perl -w use strict; use 5.24.1; use Data::Dumper qw(Dumper); use XML::Mini::Document; $Data::Dumper::Terse=1; my($xml)='<insert valid xml string here>'; my($hash_ref)=&convert_xml_to_hash($xml); my $data=eval($hash_ref); say Dumper($data); exit; sub convert_xml_to_hash { my($xml)=(@_); my($hash)={}; if($xml) { my($xml_object)=XML::Mini::Document->new(); $xml_object->parse($xml); my($output)=$xml_object->toHash(); $hash=Dumper($output); } return($hash); }

Here is a sample of the output:

... 'billing_address' => { 'province' => 'New Brunswick', 'city' => 'Moncton', 'company_nam' => '', # Should be company_name 'phone' => '5065551212', 'address_' => '', # Should be address_2 'country_code' => 'CA', 'first_name' => 'Joe', 'address_1' => '123 Somestreet', 'email' => '', 'country' => 'Canada', 'postal_code' => 'E4E 4E4', 'last_name' => 'Blow', 'province_code' => 'NB' }, 'fee_line' => '', # should be fee_lines 'shipping_tax' => '0.00', ...

At first I thought that they were just some careless typos, but after some investigation I realized that was not the case.

Any insight into this problem or a nudge in the right direction would be greatly appreciated.


In reply to XML to Hash Truncating Keys Problem by Wayne

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2021-06-21 17:27 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (99 votes). Check out past polls.