Maxim has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am trying to remove a specific elements from an XML file. In my XML file I can have more than one the same element, example <channel>.I can delete the value with this     delete $category->{'subcategory'}[$countersubs]{'product'}[$counterprod]{'channel'}[$counterchan] || die "Error";. When I try anohter way like delete $category->{'subcategory'}[$countersubs]{'product'}[$counterprod]{'channel'} || die "Error";, it delete all the elements. Where am I wrong??
Here the code
use strict; use warnings; use XML::Simple; use CGI ':standard'; use Data::Dumper; #Variable my $countersubs = 0; my $counterchan = 0; my $subprod = 'asa'; my $counterprod = 0; my $prodid = '3'; my $schan = 'j'; print header; my $arrays = [qw/subcategory name packaging_qty receipe description pr +oduct paragraph comments channel brand conditionning product_id headi +ng photo/]; print "Test2\n"; my $category = XMLin(\*DATA, keyattr => 1, forcearray => $arrays) || d +ie "can't open the file"; foreach my $subcategory (@{$category->{'subcategory'}}) { my $inssub = $category->{'subcategory'}[$countersubs]{'name'} || d +ie "An error occur!"; if ($inssub eq $subprod) { foreach my $product (@{$subcategory->{'product'}}) { foreach my $product_id (@{$product->{'product_id'}}) { if ($product_id eq $prodid) { foreach my $channel (@{$product->{'channel'}}) { if ($channel eq $schan) { delete $category->{'subcategory'}[$counter +subs]{'product'}[$counterprod]{'channel'} || die "Error"; print XMLout($category, xmldecl => '<?xml +version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href +="products.xsl"?>', rootname =>'category'); exit 1; } $counterchan++; } } } $counterprod++; } } $countersubs++; } __DATA__ <?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" + href="products.xsl"?> <category name="Coffee"> <subcategory name="loop"> <comments> <paragraph>jk</paragraph> </comments> <product recipe="/recipes/coffee1333.html">Coffee<brand>d</brand> <channel>d</channel> <channel>g</channel> <conditionning unit="d">d</conditionning> <description>d</description> <packaging_qty>d</packaging_qty> <product_id>d</product_id> </product> </subcategory> <subcategory name="asa"> <comments> <paragraph>dsfs</paragraph> </comments> <product recipe="/recipes/coffee1333.html">Coffee<brand>3</brand> <channel>j</channel> <channel>j</channel> <conditionning unit="3">3</conditionning> <description>3</description> <packaging_qty>3</packaging_qty> <product_id>3</product_id> </product> </subcategory> </category> </readmore>

Maxim

Replies are listed 'Best First'.
Re: How do I delete a specific element from an XML file
by pg (Canon) on Oct 31, 2004 at 03:37 UTC

    That's what supposed to happen. If you do a Data::Dumper of $category, you will see that $category->{'subcategory'}[$countersubs]{'product'}[$counterprod]{'channel'} is a hash element that contains an array ref.

    delete $category->{'subcategory'}[$countersubs]{'product'}[$counterpro +d]{'channel'}[counterchan]

    deletes the counterchan th element of the array. (I think "counterchan" is a typo of "$counterchan")

    delete $category->{'subcategory'}[$countersubs]{'product'}[$counterpro +d]{'channel'}

    deletes the hash element, which is the entire array.

      A side note: delete an array element does not shift index. Try the following example:

      use Data::Dumper; use strict; use warnings; my @a = (1,2,3,4,5); print Dumper(\@a); delete $a[1]; #splice(@a, 1,1); print Dumper(\@a);

      It gives you:

      $VAR1 = [ 1, 2, 3, 4, 5 ]; $VAR1 = [ 1, undef, 3, 4, 5 ];

      The array after deletion does not become something like

      $VAR1 = [ 1, 3, 4, 5 ];

      as splice does.

        Sorry about the spelling mistake I made for $counterchan. I understand in general what you said with the example but how do I implemented?
        I found the problem, it come from with the exit command. WIth this it works well
        delete $category->{'subcategory'}[$countersubs]{'product'}[$counterpro +d]{'channel'}[$counterchan]
        only when all loops is finished their process.
        Probem solve! Just need to put the command "exit 1" outside of the loop with a condition and everything works welll.
        Example:exit 1 if isDel;