XML::Simple's name is misleading, it sounds like "a simple module for complete XML handling", but it's not - it's more like "simplistic XML handling for simple XML". I find it's great for reading simple XML config files that have been designed to work with XML::Simple, but it is not an all-purpose solution, and in your case is very likely not appropriate because, among several other things, it very often doesn't maintain an XML document's structure when reading a file and writing it back.

Anyway, I'm inclined to agree with the other monks' suggestions for XML::Twig, which is great when you want to process a file piece by piece. For the case you describe, if the file isn't so big that loading it into memory is too expensive, then XML::LibXML is fine too. For example, the following deletes the <bar> element if its child <quz>'s text content is "baz".

use warnings; use strict; use XML::LibXML; my $dom = XML::LibXML->load_xml(IO=>\*DATA); print "### Before:\n", $dom->toString; for my $el ($dom->findnodes('/foo/bar/quz')) { $el->textContent eq 'baz' and $el->parentNode->unbindNode; } print "### After:\n", $dom->toString; __DATA__ <foo> <bar> <quz>baz</quz> </bar> <x/> <bar> <quz>abc</quz> <y/> </bar> </foo>

Output:

### Before: <?xml version="1.0"?> <foo> <bar> <quz>baz</quz> </bar> <x/> <bar> <quz>abc</quz> <y/> </bar> </foo> ### After: <?xml version="1.0"?> <foo> <x/> <bar> <quz>abc</quz> <y/> </bar> </foo>

In reply to Re: XML parsing by Anonymous Monk
in thread XML parsing by mading0

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.