我是 xml twig 的新手...如何阅读和更改<!DOCTYPE article SYSTEM "loose.dtd">
and <?xml version="1.0" encoding="UTF-8"?>
。我如何修改这个标签..因为我不知道如何在 xml::Twig 中读取和更改这个标签...
我的输入:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE art SYSTEM "loose.dtd">
<art>
<fr>
<p>Text</p>
<p>Text</p>
</fr>
<fr>
<p>Text</p>
<p>Text</p>
</fr>
</art>
我需要输出为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DTD>
<Contents type="<!DOCTYPE article SYSTEM "loose.dtd>"/>
</DTD>
<art>
<fr>
<p>Text</p>
<p>Text</p>
</fr>
<fr>
<p>Text</p>
<p>Text</p>
</fr>
</art>
怎样才能改变<?xml ?> and <!Doctype>
标签,你能帮助这个过程吗?
您可以尝试以下操作(代码已注释)。理解它的重要一点是创建一个新的twig
,复制所有要保留的元素并创建其更改的内容:
#!/usr/bin/env perl
use warnings;
use strict;
use XML::Twig;
## Create a twig based in an input xml file.
my $twig = XML::Twig->new;
$twig->parsefile(shift);
## Create a new twig that will be the output.
my $new_twig = XML::Twig->new( pretty_print => 'indented' );
## Create a root tag.
$new_twig->set_root( XML::Twig::Elt->new( 'root' ) );
## Create the xml processing instruction.
my $e = XML::Twig::Elt->new( 'k' => 'v' );
$e->set_pi( 'xml', 'version="1.0" encoding="UTF-8" standalone="yes"' );
$e->move( before => $new_twig->root );
## Copy the whole tree from the old twig.
my $r = $twig->root;
$r->paste( first_child => $new_twig->root );
## Copy the doctype from the old twig to the new one.
my $contents_elt = XML::Twig::Elt->new( Contents => { type => $twig->doctype } );
my $dtd_elt = XML::Twig::Elt->new( DTD => '#EMPTY' );
$contents_elt->move( last_child => $dtd_elt );
$dtd_elt->move( first_child => $new_twig->root );
## Print the whole twig created.
$new_twig->print;
像这样运行它:
perl script.pl xmlfile
得出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root>
<DTD>
<Contents type="<!DOCTYPE art SYSTEM "loose.dtd">
"/>
</DTD>
<art>
<fr>
<p>Text</p>
<p>Text</p>
</fr>
<fr>
<p>Text</p>
<p>Text</p>
</fr>
</art>
</root>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)