我通过 perl 脚本(如下所示)使用 XML::LibXML 创建以下 XML 文件:
more test.xml
<?xml version="1.0"?>
<books>
<computer/>
</books>
我的问题:如何删除“xml版本标题”:
<?xml version="1.0"?>
来自 test.xml 文件?在 perl 脚本中使用 DOM 命令?
为了仅获取 text.xml 文件中的以下行:
<books>
<computer/>
</books>
耶尔
#perl 脚本:
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new;
my $root = $doc->createElement('books');
$doc->setDocumentElement($root);
my $computer = $doc->createElement('computer');
$root->appendChild($computer);
$doc->toFile('/var/tmp/test.xml');
最佳答案
好的,关于我之前的评论,我现在找到了解决方案。
似乎toFile
绕过了$skipXMLDeclaration
,而toString
却没有。因此,以下工作有效:
$XML::LibXML::skipXMLDeclaration = 1;
my $doc = XML::LibXML::Document->new;
# create your document
print $doc->toString;
(非常小的)缺点是您必须自己编写文件。
关于perl + DOM + 使用 XML::LibXML + 如何从 XML 文件中删除 xml 版本标题?与 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236028/