Maxim has asked for the wisdom of the Perl Monks concerning the following question:
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>
|
|---|
| 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 | |
by pg (Canon) on Oct 31, 2004 at 03:50 UTC | |
by Maxim (Sexton) on Oct 31, 2004 at 05:18 UTC | |
by Maxim (Sexton) on Nov 02, 2004 at 04:02 UTC | |
by Maxim (Sexton) on Nov 02, 2004 at 06:45 UTC |