Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

Wayne:

It would've been easier had you actually included a bit of XML. I went ahead and installed XML::Mini::Document and ran it, but as you can see, I'm not experiencing the problem:

$ cat pm_1231805_to_hash.pl #!/usr/bin/perl -w use strict; use warnings; use 5.24.1; use Data::Dumper qw(Dumper); use XML::Mini::Document; $Data::Dumper::Terse=1; my $xml = <<EOXML; <shipping_tax>0.00</shipping_tax> <fee_lines></fee_lines> <billing_address> <city>Moncton</city> <province_code>NB</province_code> <country>Canada</country> <address_2></address_2> <email>someone\@somewhere.com</email> <country_code>CA</country_code> <province>New Brunswick</province> <phone>5065551212</phone> <first_name>Joe</first_name> <address_1>123 Somestreet</address_1> <postal_code>E4E 4E4</postal_code> <company_name></company_name> <last_name>Blow</last_name> </billing_address> EOXML 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); } Roboticus@Waubli ~ $ perl pm_1231805_to_hash.pl { 'shipping_tax' => '0.00', 'fee_lines' => '', 'billing_address' => { 'province' => 'New Brunswick', 'country' => 'Canada', 'address_2' => '', 'last_name' => 'Blow', 'company_name' => '', 'province_code' => 'NB', 'phone' => '5065551212', 'country_code' => 'CA', 'email' => 'someone@somewhere.com', 'postal_code' => 'E4E 4E4', 'city' => 'Moncton', 'address_1' => '123 Somestreet', 'first_name' => 'Joe' } }

Perhaps you have either (a) some wonky data, or (b) a bit of code you've not shown us is frobnicating it somewhere.

Edit: I forgot to mention that I didn't build the input by hand. Since you at least provided some output, I edited the output to make it what you wanted. I then used XML::Mini::Document to rebuild the input (code in the readmore tags below).

#!/usr/bin/perl -w use warnings; use strict; use XML::Mini::Document; use 5.24.1; my $xmlDoc = XML::Mini::Document->new(); my $data = { 'billing_address' => { 'province' => 'New Brunswick', 'city' => 'Moncton', 'company_name' => '', # Should be company_name 'phone' => '5065551212', 'address_2' => '', # Should be address_2 'country_code' => 'CA', 'first_name' => 'Joe', 'address_1' => '123 Somestreet', 'email' => 'someone@somewhere.com', 'country' => 'Canada', 'postal_code' => 'E4E 4E4', 'last_name' => 'Blow', 'province_code' => 'NB' }, 'fee_lines' => '', # should be fee_lines 'shipping_tax' => '0.00', }; $xmlDoc->fromHash($data); print $xmlDoc->toString();

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: XML to Hash Truncating Keys Problem by roboticus
in thread XML to Hash Truncating Keys Problem by Wayne

Title:
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?
Username:
Password:

What's my password?
Create A New User
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-06-21 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (97 votes). Check out past polls.

    Notices?