Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1062781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:40:52+00:00 2026-05-16T18:40:52+00:00

I am using XML::Simple for parsing a XML file. Code is given below with

  • 0

I am using XML::Simple for parsing a XML file. Code is given below with XML file,

use Tie::IxHash;

tie %$data, "Tie::IxHash";

use XML::Simple;
use Data::Dumper;

$xml = new XML::Simple;
$data = $xml->XMLin("ship_order.xml");
print Dumper($data);

XML file, (ship_order.xml)

<?xml version="1.0" encoding="UTF-8" ?> 

<shipment>
    <shiptoaddress>
        <name>Prasad</name> 
        <address>AnnaNagar</address> 
    </shiptoaddress> 
    <items>
        <quantity>5</quantity> 
        <price>100</price> 
    </items> 
    <items>
        <quantity>6</quantity> 
        <price>50</price> 
    </items>
    <num_of_items>2</num_of_items>
</shipment>

Output is not coming in order, even though I am using Tie::IxHash module.

My output:

$VAR1 = {
          'num_of_items' => '2',
          'shiptoaddress' => {
                             'name' => 'Prasad',
                             'address' => 'AnnaNagar'
                           },
          'items' => [
                     {
                       'quantity' => '5',
                       'price' => '100'
                     },
                     {
                       'quantity' => '6',
                       'price' => '50'
                     }
                   ]
        };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:40:53+00:00Added an answer on May 16, 2026 at 6:40 pm

    Ah, but you aren’t using Tie::IxHash. Or more accurately, you start off using Tie::IxHash and then destroy it:

    $data = $xml->XMLin("ship_order.xml");
    

    This line discards the hash reference you created and assigns one from the method call to $data.

    If you care about the order of the items (and you probably shouldn’t have to since any decent XML format will include an attribute that tells you the order), you will need to use a parser that returns an object, not a data structure. The object will know the order the items were seen and provide you with a children method that returns them.

    Alternatively, you could build the data structure yourself:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use XML::Twig;
    
    my $shipment;
    my $t = XML::Twig->new(
        twig_handlers => {
            shiptoaddress => sub {
                my ($t, $elt) = @_;
    
                $shipment->{name}    = $elt->first_child("name")->text;
                $shipment->{address} = $elt->first_child("address")->text;
    
                $t->purge;
            },
            items => sub {
                my ($t, $elt) = @_;
    
                push @{$shipment->{items}}, {
                    quantity => $elt->first_child("quantity")->text,
                    price    => $elt->first_child("price")->text,
                };
    
                $t->purge;
            },
        },
    );
    
    $t->parse(join "", <DATA>); #FIXME: use parsefile later
    
    use Data::Dumper;
    print Dumper $shipment;
    
    __DATA__
    <?xml version="1.0" encoding="UTF-8" ?> 
    
    <shipment>
        <shiptoaddress>
            <name>Prasad</name> 
            <address>AnnaNagar</address> 
        </shiptoaddress> 
        <items>
            <quantity>5</quantity> 
            <price>100</price> 
        </items> 
        <items>
            <quantity>6</quantity> 
            <price>50</price> 
        </items>
        <num_of_items>2</num_of_items>
    </shipment>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sample code written for parsing xml file using javax.xml package. it uses
I am using the following code for parsing an XML file. But I don't
I am parsing itunes library XML file using xslt grammar. Here's sample code void
I am parsing XML from the web using Android. The code below shows a
I am using the XML::Simple module to parse an XML file. When I run
I'm creating an XML file using Perl and XML::Simple module. I successfully create the
I'm trying to validate a simple XML file against an XML schema using the
I am using php's simple xml and xpath to parse an rdf xml file
Ho to make this simple xml with php using DOM? Full code will be
Using OmniXML package, is it possible to store XML code inside another XML file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.