开发者

Perl / LibXML : keep closing tags when serializing

开发者 https://www.devze.com 2023-03-08 23:18 出处:网络
By default, Li开发者_JAVA百科bXML will render empty tags as <tag />, but I need to have it render them as <tag></tag>.

By default, Li开发者_JAVA百科bXML will render empty tags as <tag />, but I need to have it render them as <tag></tag>.

Is there an option I missed in the documentation, or do I have to tweek the output with regexp replacements (or any other solution you might know of) ?

I'm looking for a better way of doing it in the place of:

$xml = $dom->serialize(0);
$xml =~ s/<([a-z]+)([^>]*?)\/>/<$1$2><\/$1>/gsi;


LibXML has a formerly documented feature, that might be considered deprecated as it's not in the documentation for the latest version, but it's still in the test files, so it might work.

All serialization functions understand the flag setTagCompression. if this Flag is set to 1 empty tags are displayed as <foo></foo> rather than <foo/>.

my $xml = do {
    local $XML::LibXML::setTagCompression = 1;
    $doc->toString();
};


How about:

use XML::LibXML;

my $x = XML::LibXML->new();
my $d = $x->load_xml(string => "<xml><foo/></xml>");

print $d->toString;

print qq{<?xml version="1.0"?>\n} . $d->toStringHTML();'

yields:

<?xml version="1.0"?>
<xml><foo/></xml>

<?xml version="1.0"?>
<xml><foo></foo></xml>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号