Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Parsing Issue

Status
Not open for further replies.

youthman

Programmer
Apr 28, 2004
49
US
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
 
Hi

Supposing I reconstructed correctly your data structure ( next time please post a functional piece of code we can use to test your problem ) :
Perl:
[b]my[/b] [navy]$xml[/navy] [teal]=[/teal] [teal]{[/teal]
  [green][i]'Items'[/i][/green] [teal]=>[/teal] [teal]{[/teal]
    [green][i]'Item'[/i][/green] [teal]=>[/teal] [teal]{[/teal]
      [green][i]'ItemAttributes'[/i][/green] [teal]=>[/teal] [teal]{[/teal]
        [green][i]'Size'[/i][/green] [teal]=>[/teal] [green][i]'Single'[/i][/green][teal],[/teal]
        [green][i]'ProductGroup'[/i][/green] [teal]=>[/teal] [green][i]'Products'[/i][/green][teal],[/teal]
        [green][i]'Feature'[/i][/green] [teal]=>[/teal] [teal][[/teal]
          [green][i]'Blah One Blah'[/i][/green][teal],[/teal]
          [green][i]'Blah Two Blah'[/i][/green][teal],[/teal]
          [green][i]'Blah Three Blah'[/i][/green][teal],[/teal]
          [green][i]'Blah Four Blah'[/i][/green][teal],[/teal]
        [teal]][/teal]
      [teal]}[/teal]
    [teal]}[/teal]
  [teal]}[/teal]
[teal]}[/teal][teal];[/teal]

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

[b]print[/b] [green][i]"Size\t$size\n"[/i][/green][teal];[/teal]
[b]print[/b] [green][i]"Group\t$group\n"[/i][/green][teal];[/teal]
[b]print[/b] [green][i]"Feat[1]\t$feat[1]\n"[/i][/green][teal];[/teal]
[b]print[/b] [green][i]"Feats 3\t$feats3\n"[/i][/green][teal];[/teal]
Code:
Size	Single
Group	Products
Feat[1]	Blah Two Blah
Feats 3	Blah Three Blah

Feherke.
feherke.github.io
 
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";

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
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.

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


: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top