Yes, XML is 'human readable' (ie, plain old text), but I don't think it's a very robust solution to make changes to an XML document simply with a s/// operation skimming its way through the file. You have to weigh the risks of messing it up by under-engineering the solution against the difficulty and value of developing a properly engineered solution.

The good news is that there are some excellent XML parsing tools that virtually eliminate the "difficulty" of implementing a well-engineered solution. XML::Simple is one, but I prefer XML::Twig, because it handles some peculiar XML cases that make 'Simple choke, and because it still provides a very simple interface, similar to that of XML::Simple. With XML::Twig you can, in very few easy lines of code, grab the entire XML document (or portions thereof) and have it automagically parsed into a Perl datastructure. Then after modifying the specific elements within that datastructure that need to be changed, one or two more lines of code will dump that datastructure back into a properly formed XML document.

Because the XML::Twig approach is so easy to use, and because it represents a "properly engineered" solution, there is simply virtually no reason not to favor it over a less robust "regexp" solution.

The only problem I see is with regard to keeping indents and line breaks from the original file in the same exact places. XML::Twig will output clean XML, but it might not follow the existing formatting 100%. ...nor should it matter; XML, while human readable, is ultimately there for the machines to use. You can guarantee that an XML document output by XML::Twig will retain just as much human readability as the original, but you cannot guarantee that it will look 100% identical.


Dave


In reply to Re: Replacing things in XML files by davido
in thread Replacing things in XML files by John M. Dlugosz

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.