2

I have XML that contains ' for ', etc. When I parse it in using XML::Twig and then print it out again, all the ' are printed as '. Also, XML::Twig seems to reorder attributes to put them in alphabetical order. From the point of view of XML, these are equivalent, but, I want to make a small set of changes to XML and use diff to confirm the only changes made are the ones I intended. Is there a way to get XML::Twig to change nothing except what I explicitly change?

Here's my XML:

<?xml version="1.0" encoding="utf-8"?>
<System>
  <P C="C" B="B" A="A">&apos;&lt;&gt;&quot;&amp;</P>
  <P A="A" B="B" C="C">'&lt;>"&amp;</P>
</System>

And the perl:

my $twig = new XML::Twig(KeepSpaces => 'true');
$twig->parsefile("test.xml");
$twig->print();

And here's what gets printed:

<?xml version="1.0" encoding="utf-8"?>
<System>
  <P A="A" B="B" C="C">'&lt;>"&amp;</P>
  <P A="A" B="B" C="C">'&lt;>"&amp;</P>
</System>
4

1 回答 1

7

似乎该keep_encoding选项可以解决问题:

use strict;
use warnings;
use XML::Twig;

my $twig = XML::Twig->new( 
                    keep_spaces     => 1, 
                    keep_encoding   => 1, 
                    keep_atts_order => 1,
);
$twig->parsefile('test.xml');
$twig->print;

更新:针对 mirod 的评论进行了改进。

于 2013-07-09T13:09:50.897 回答