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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:53:03+00:00 2026-06-10T10:53:03+00:00

I’m trying to extract a small amount of data from an XML file into

  • 0

I’m trying to extract a small amount of data from an XML file into a csv file using perl and XML::Simple.

Here is an edited version of the data:

<?xml version="1.0" encoding="UTF-8"?>
<orders xmlns="http://www.demandware.com/xml/impex/order/2006-10-31">
    <order order-no="W100148941">
        <order-date>2011-08-22T16:15:47.000Z</order-date>
        <custom-attributes>
            <custom-attribute attribute-id="basket_notes">bnotes974211</custom-attribute>
            <custom-attribute attribute-id="omOrderID">974211</custom-attribute>
        </custom-attributes>
    </order>
</orders>

using this script:

#!/usr/bin/perl

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

$xml = new XML::Simple;
$data = $xml->XMLin("$ARGV[0]", ForceArray=>1);


print Dumper($data);
foreach $o (@{$data->{order}}) {
    print "$ARGV[1]", ",";
    print "$ARGV[2]", ",";
    print "$ARGV[3]", ",";
    print "$ARGV[4]", ",";
    print $o->{"order-no"}, ",";
    print $o->{"order-date"}, ",";
    foreach my $o ( @{ $data->{'custom-attribute'} } ) {
        print 'in level 1';
        foreach my $attr ( @{ $data->{'custom-attribute'} } ) {
            print 'in level 2';
            if ( $attr->{'attribute-id'} eq 'basket_notes' ) {
                print '"', $data->{'content'}, '"', ",";
            }
        }
    }
    print "\n";
}

gets me this output:

,,,,W100148941,ARRAY(0x7f7f63a524c0),

Not using the ForceArray option XMLin will replace the ARRAY(…) above with the correct value, but won’t work with files with only one data element, and, as is evident, this code never does make into the custom attribute array to print anything.

What am I doing wrong?

update:

changing the looping code in the above to this:

foreach $o (@{$data->{order}})
{
print "$ARGV[1]", ",";
print "$ARGV[2]", ",";
print "$ARGV[3]", ",";
print "$ARGV[4]", ",";
print $o->{"order-no"}, ",";
#print $o->{"order-date"}, ",";
print $o->{"order-date"}->[0], ",";
foreach my $o ( @{ $data->{'custom-attributes'} } ) {
    print 'in level 1';
   foreach my $attr ( @{ $o->{'custom-attribute'} } ) {
        print 'in level 2';
        if ( $attr->{'attribute-id'} eq 'omOrderID' ) {
            print '"', $data->{'content'}, '"', ",";
        }
    }
}

print "\n";
}

yields this:

,,,,W100148941,2011-08-22T16:15:47.000Z,

It would appear that the code is just not getting into the custom-attributes loop, and I don’t know why.

  • 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-10T10:53:05+00:00Added an answer on June 10, 2026 at 10:53 am

    Your problem is that “order-date” -due to ForceArray – is ALSO getting forced to be an arrayref, as you an see from your already-existing Dumper output:

    ...
         'order-date' => [
                         '2011-08-22T16:15:47.000Z'
                         ],
    

    Therefore, you need to do one of 2 things:

    • If order-date will always be a single value, hard-code printing the first array value:

      print $o->{"order-date"}->[0], ",";
      
    • If order-date will always be a single value, change your constructor arguments by passing a more detailed ForceArray instructions.

      XML::Simple POD shows that aside from a simple ForceArray=>1 option, you can also pass a list of limited tags you want to force into array (e.g. ForceArray => [ "custom-attributes", "custom-attribute" ])

      • If order-date can have multiple tags, simply print it in a loop as you already do with other multiple tags below:

        foreach my $order_date ( @{ $data->{‘order-date’} } ) {
        print “$order_date,”


    Also, you have a couple of bugs in your nested loops.

    Your first loop should be

    foreach my $o ( @{ $data->{'custom-attributes'} } ) { # You had "attribute"
    

    And the second loop should loop over the sub-structures of that:

        foreach my $attr ( @{ $o->{'custom-attribute'} } ) { # instead of $data->...
    

    Leaving all that aside, from my fairly considerable experience, converting XML top a flat file (CSV) is somewhat of a bad idea, to put it mildly. Please seriously consider whether you are doing the right thing at all.

    There is no way to properly or easily map the data without crafty encoding; and decoding that crafty encoding later is no easier than simply reading in the XML again.

    • If you need to convert it so it can be readable by another program, keep the XML or convert to JSON

    • If you need to convert it to show to a human, use Data::Dumper or some other pretty printer

    • If you need to show it to a human as a GUI, develop a good GUI to match your data structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I am currently running into a problem where an element is coming back from
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm making a simple page using Google Maps API 3. My first. One marker

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.