×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

XML Parsing Issue

XML Parsing Issue

XML Parsing Issue

(OP)
I have an XML File that looks (in part - this section is several levels down) like:
'Size' => 'Single',
'ProductGroup' => 'Products',
'Feature' => [
'Blah Blah Blah',
'Blah Blah Blah',
'Blah Blah Blah',
'Blah Blah Blah',
],

etc.

When I parse the file that is received, I can gain access to Size, and Product group with this code using XML Parser:

my $size = $xml->{Items}->{Item}->{ItemAttributes}->{Size};
my $group = $xml->{Items}->{Item}->{ItemAttributes}->{ProductGroup};

The problem I am experiencing is that when I pull the Features using the same technique, I get an array back. I have tried to parse this array with a SPLIT command:

my @feat = $xml->{Items}->{Item}->{ItemAttributes}->{Feature};
($feats1,$feats2,$feats3,$feats4,$feats5) = split (/,/, @feat);

I cannot seem to do anything with the data that I receive, and when I turn the @feat into a standard $feat = $xml->{Items}-> ... and try to print the result, all I get is ARRAY{...}

I know that this has to be simple, and I am just overlooking it, but can anyone else help with this?


Thanks

RE: XML Parsing Issue

Hi

Supposing I reconstructed correctly your data structure ( next time please post a functional piece of code we can use to test your problem ) :

CODE --> Perl

my $xml = {
  'Items' => {
    'Item' => {
      'ItemAttributes' => {
        'Size' => 'Single',
        'ProductGroup' => 'Products',
        'Feature' => [
          'Blah One Blah',
          'Blah Two Blah',
          'Blah Three Blah',
          'Blah Four Blah',
        ]
      }
    }
  }
};

my $size = $xml->{Items}->{Item}->{ItemAttributes}->{Size};
my $group = $xml->{Items}->{Item}->{ItemAttributes}->{ProductGroup};
my @feat = @{$xml->{Items}->{Item}->{ItemAttributes}->{Feature}};
($feats1,$feats2,$feats3,$feats4,$feats5) = @feat;

print "Size\t$size\n";
print "Group\t$group\n";
print "Feat[1]\t$feat[1]\n";
print "Feats 3\t$feats3\n"; 

CODE --> output

Size	Single
Group	Products
Feat[1]	Blah Two Blah
Feats 3	Blah Three Blah 

Feherke.
feherke.github.io

RE: XML Parsing Issue

The same, just a little more efficient:

CODE -->

my $feat = $xml->{Items}->{Item}->{ItemAttributes}->{Feature};
($feats1,$feats2,$feats3,$feats4,$feats5) = @$feat;

print "Feat[1]\t$$feat[1]\n";
print "Feats 3\t$feats3\n"; 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: XML Parsing Issue

(OP)
All of this works great, until I get to one that has Feature Atributes ... Sometimes, the data looks like this:
my $xml = {
'Items' => {
'Item' => {
'ItemAttributes' => {
'Size' => 'Single',
'ProductGroup' => 'Products',
'Feature' => [
{
'Content' => 'Blah One Blah',
'Blah Two Blah',
'Source => 'Blah Three Blah',
'Link' => 'Blah Four Blah',
}
{
'Content' => 'Blah One Blah',
'Blah Two Blah',
'Source => 'Blah Three Blah',
'Link' => 'Blah Four Blah',
}
}
}
}
};

When I hit one of these sections, I get an error that states "Bad index while coercing array into hash" I have tried everything I can think of to make this work, but the structure is not always the same.

RE: XML Parsing Issue

You need to understand how references work in perl.
my $feat = $xml->{Items}->{Item}->{ItemAttributes}->{Feature};
returns a reference to an array, as is seen from the brackets in your data structure.
Now @$feat returns the content of that array in list context, and the number of elements in scalar context and $$feat[$i] returns the element $i.
But the array @$feat contains in turn references to hashes, as is seen from the curls enclosing them.
So keys%$$feat[0] returns the key list of the first hash and $$$feat[0]{Content} returns the 'Content' of the first hash.
Sorry, I'm not accustomed to the $xml->{Items} way of dereferencing, I would've written the first line as
my $feat = $$xml{Items}{Item}{ItemAttributes}{Feature};

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close