in reply to XML cut and paste

XML::Simple is good enough in this case. Most likely you do't know what the data structure should look like after you moved the product id around, in terms of how XML::Simple wants it to be. That is easy, do a copy and paste manually, read in those two files, one with product id at its current place, and one with product id at the place you want, observe carefully the difference, then you will know how.

Replies are listed 'Best First'.
Re^2: XML cut and paste
by pg (Canon) on Oct 17, 2004 at 18:59 UTC

    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>
      What you gave e, it doesn't move the product id "800.55.01" to the subcategory "Grand Abrica". I am trying now to use XML::twig, which I found an useful example from the documentation. Thanks by the way to help me.

      Maxim