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 6025567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:19:09+00:00 2026-05-23T04:19:09+00:00

I have XML file (output of some application) like this: <data> <call function=get_user_data> <output>

  • 0

I have XML file (output of some application) like this:

<data>
    <call function="get_user_data">
        <output>
            <integer name="my_id" value="-31" />
            <string name="login" value="root" />
            <integer name="ip_list" value="2" />
            <array name="i">
                <item>
                    <ip_address name="user_ip" value="0.0.0.0" />
                    <ip_address name="user_mask" value="0.0.0.0"/>
                </item>
                <item>
                    <ip_address name="user_ip" value="94.230.160.230" />
                    <ip_address name="user_mask" value="255.255.255.0"/>
                </item>
            </array>
            <integer name="modules" value="2" />
            <array name="i">
                <item>
                    <string name="module_name" value="core" />
                </item>
                <item>
                    <string name="module_name" value="devices" />
                </item>
            </array>
            <integer name="addition_modules" value="0"/>
            <array name="i"/>
        </output>
    </call>
</data>

I need to parse it for this perl structure:

$data = {
    my_id => -31,
    login => "root",
    ip_list => [
        {
            user_ip => "0.0.0.0",
            user_mask => "0.0.0.0"
        },
        {
            user_ip => "94.230.160.230",
            user_mask => "255.255.255.0"
        }
    ],
    modules => [
        {
            module_name => "core"
        },
        {
            module_name => "devices"
        }
    ],
    addition_modules => []
}

Help me, plz, to make it!

  • 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-23T04:19:10+00:00Added an answer on May 23, 2026 at 4:19 am

    This is an awful XML format. It’s redundant: why give the number of elements for each array, it’s there, and the hierarchy is not well defined: the name of the array (and, if you must have it, its number of elements) should be an attribute of the array, not an element before it at the same level.

    Anyway… your format is so specific that I doubt you can use the usual XML::Simple here, so I pulled out XML::Twig, and I think this will do:

    #!/usr/bin/perl 
    
    use strict;
    use warnings;
    
    use XML::Twig;
    use Test::More tests => 1;
    
    my $expected= {
        my_id => -31,
        login => "root",
        ip_list => [
            { user_ip => "0.0.0.0",
              user_mask => "0.0.0.0"
            },
            { user_ip => "94.230.160.230",
              user_mask => "255.255.255.0"
            }
        ],
        modules => [
            { module_name => "core" },
            { module_name => "devices" }
        ],
        addition_modules => []
    };
    
    my $data={};
    
    my $t=XML::Twig->new( twig_handlers => { 'integer[@name="my_id"]' => sub { add_field( $data, $_)},
                                             'string[@name="login"]' => sub { add_field( $data, $_)},
                                             array => sub { array( $data, @_); },
                                           },
                  )
             ->parse( \*DATA); # replace with parsefile( 'file.xml') to parse a file
    
    is_deeply( $data, $expected, 'one test to rule them all');
    
    
    sub array
      { my( $data, $t, $array)= @_;
        my $name= $array->prev_sibling( 'integer')->att( 'name');
        $data->{$name}=[];
        foreach my $item ($array->children( 'item'))
          { my $item_data={};
            foreach my $child ($item->children)
              { add_field( $item_data, $child); }
            push @{$data->{$name}}, $item_data;
          }
      }
    
    
    # get a name/value pair of attributes and add it to a hash, which
    # could be the overall $data or an element in an array
    sub add_field
      { my( $data, $elt)= @_;
        $data->{$elt->att( 'name')}= $elt->att( 'value');
      }
    
    
    
    __DATA__
    <data>
        <call function="get_user_data">
            <output>
                <integer name="my_id" value="-31" />
                <string name="login" value="root" />
                <integer name="ip_list" value="2" />
                <array name="i">
                    <item>
                        <ip_address name="user_ip" value="0.0.0.0" />
                        <ip_address name="user_mask" value="0.0.0.0"/>
                    </item>
                    <item>
                        <ip_address name="user_ip" value="94.230.160.230" />
                        <ip_address name="user_mask" value="255.255.255.0"/>
                    </item>
                </array>
                <integer name="modules" value="2" />
                <array name="i">
                    <item>
                        <string name="module_name" value="core" />
                    </item>
                    <item>
                        <string name="module_name" value="devices" />
                    </item>
                </array>
                <integer name="addition_modules" value="0"/>
                <array name="i"/>
            </output>
        </call>
    </data>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have a problem extrating some data out of this XML file: <?xml version=1.0
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
I have created XML file,but I can't view it/output it.I know there is no
I have an XML input file and I'm trying to output the result of
I am currently watching an XML file from log4j output. I have a custom
I have an xml file providing data for a datagrid in Flex 2 that
I have an XML file which is transformed with XSL. Some elements have to
I have an XML file which has some values in child Element aswell in
I have an XML file. There is some blank line in the file. How

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.