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

In reply to How do I delete a specific element from an XML file by Maxim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.