in reply to Re: XML cut and paste
in thread XML cut and paste
I followed what I said to you, and here is the code, and result:
use XML::Simple; use Data::Dumper; use strict; use warnings; my $ref = XMLin("test1.xml"); my $subcats = $ref->{'subcategory'}; for my $subcat (keys(%$subcats)) { my $id; my $products = $subcats->{$subcat}->{'product'}; if (ref($products) eq 'ARRAY') { $id = $products->[0]->{'product_id'}; for (0 .. $#{@$products}) { delete $products->[$_]->{'product_id'}; } } elsif (ref($products) eq 'HASH') { $id = $products->{'product_id'}; delete $products->{'product_id'}; } $subcats->{$subcat}->{'product_id'} = $id; } my $xml = XMLout($ref, KeepRoot=>1, NoAttr => 1); print $xml;
<subcategory> <Grand Arabica> <photo>images/arabica.jpg</photo> <product> <brand>Legal</brand> <channel>A</channel> <channel>D</channel> <channel>S</channel> <conditionning> <content>250</content> <unit>gr</unit> </conditionning> <content>Coffee </content> <description>Grand Arabica</description> <packaging_qty>12</packaging_qty> </product> <product_id>802.55.01</product_id> </Grand Arabica> <Prestige> <photo>images/prestige.jpg</photo> <product> <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <conditionning> <content>250</content> <unit>gr</unit> </conditionning> <content>Coffee </content> <description>Legal Prestige</description> <packaging_qty>12</packaging_qty> <recipe>/recipes/coffee1.html</recipe> </product> <product> <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <conditionning> <content>500</content> <unit>gr</unit> </conditionning> <content>Coffee </content> <description>Legal Prestige Boite Arôme</description> <packaging_qty>6</packaging_qty> </product> <product_id>800.55.01</product_id> </Prestige> </subcategory>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: XML cut and paste
by Maxim (Sexton) on Oct 17, 2004 at 22:44 UTC |