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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:30:28+00:00 2026-06-01T00:30:28+00:00

I am using Perl with XML::Simple to convert a hash into an XML document.

  • 0

I am using Perl with XML::Simple to convert a hash into an XML document.

My script looks as follows:

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my $xml_simple = XML::Simple->new( NoAttr => 1,
                   KeepRoot => 1);

my $hash = { output => { 'products' => [ { 'product' => { 'titel' => 'ABN AMRO Bank hypotheken',
                                                          'owner' => 'ABN AMRO Hypotheken Groep',
                                                          'code' => 'ABN AMRO BANK R' } },
                                         { 'product' => { 'titel' => 'Aegon',
                                                          'owner' => 'AEGON Hypotheken',
                                                          'code' => 'AEGON pilot' } } ],
                         'date' => '2012-02-20'} };


my $xml = $xml_simple->XMLout( $hash );

print Dumper( $xml );

The output I am getting is:

<output>
  <date>2012-02-20</date>
  <products>
      <name>product</name>
      <code>ABN AMRO BANK R</code>
      <owner>ABN AMRO Hypotheken Groep</owner>
      <titel>ABN AMRO Bank hypotheken</titel>
  </products>
  <products>
      <name>product</name>
      <code>AEGON pilot</code>
      <owner>AEGON Hypotheken</owner>
      <titel>Aegon</titel>
  </products>
</output>

but what I am looking for is this (see the ‘product’ nodes):

<output>
  <date>2012-02-20</date>
  <products>
    <product>
      <code>ABN AMRO BANK R</code>
      <owner>ABN AMRO Hypotheken Groep</owner>
      <titel>ABN AMRO Bank hypotheken</titel>
    </product>
    <product>
      <code>AEGON pilot</code>
      <owner>AEGON Hypotheken</owner>
      <titel>Aegon</titel>
    </product>
  </products>
</output>

Is this doable with XML::Simple or should I use a different module?

  • 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-06-01T00:30:30+00:00Added an answer on June 1, 2026 at 12:30 am

    You can let XML::Simple tell you what data structure it wants

    #!/usr/bin/perl
    use strict;
    use warnings;
    use XML::Simple;
    use Data::Dumper;
    my $desired_xml = << 'END';
    <myxml>
    <output>
      <date>2012-02-20</date>
      <products>
        <product>
          <code>ABN AMRO BANK R</code>
          <owner>ABN AMRO Hypotheken Groep</owner>
          <titel>ABN AMRO Bank hypotheken</titel>
        </product>
        <product>
          <code>AEGON pilot</code>
          <owner>AEGON Hypotheken</owner>
          <titel>Aegon</titel>
        </product>
      </products>
    </output>
    </myxml>
    END
    #print $desired_xml;
    my $xml_simple = XML::Simple->new(
        NoAttr   => 1,
        KeepRoot => 1
    );
    #my $hash = XMLin( $desired_xml, forcearray => 1 );
    my $hash = {
        output => [
            {
                date     => ["2012-02-20"],
                products => [
                    {
                        product => [
                            {
                                code  => ["ABN AMRO BANK R"],
                                owner => ["ABN AMRO Hypotheken Groep"],
                                titel => ["ABN AMRO Bank hypotheken"],
                            },
                            {
                                code  => ["AEGON pilot"],
                                owner => ["AEGON Hypotheken"],
                                titel => ["Aegon"],
                            },
                        ],
                    },
                ],
            },
        ],
      };
    #print Data::Dumper->Dump( [$hash], [qw(hash)] );
    my $xml = $xml_simple->XMLout($hash);
    print Data::Dumper->Dump( [$xml], [qw(xml)] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following script: #!/usr/local/bin/perl -wT use strict; use warnings; print Content-type:
I have a Perl script to convert the XML file below into a hash:
I am using a simple Perl script to parse XML and convert it to
I currently have a Perl CGI script that parses incoming XML requests using XML::Simple
I'm trying to parse some XML in Perl using XML::Simple . The XML follows
I'm creating an XML file using Perl and XML::Simple module. I successfully create the
I am using Perl to convert some XML to JSON. If the XML attribute
I am using XML::Simple to parse a XML file. The output is a hash.(using
Using Perl's XML::LibXSLT necessitates that I use XSLT 1.0, which means that I am
I'm using XML::Simple to parse an XML file which I then want to use

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.