perl + DOM + 使用 XML::LibXML + 如何从 XML 文件中删除 xml 版本标题?使用 DOM [英] perl + DOM + use XML::LibXML + how to remove xml version title from XML file? with DOM

查看:47
本文介绍了perl + DOM + 使用 XML::LibXML + 如何从 XML 文件中删除 xml 版本标题?使用 DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XML::LibXML 通过 perl 脚本 (Showing down) 创建了以下 XML 文件:

I create the following XML file, by perl script (Showing down) , using XML::LibXML:

 more test.xml 

 <?xml version="1.0"?>
 <books>
 <computer/>
 </books>

我的问题:如何删除xml版本标题":

My question: how to remove "xml version title":

      <?xml version="1.0"?>

来自 test.xml 文件?在 perl 脚本中使用 DOM 命令?

from the test.xml file? With DOM commands in the perl script?

为了只获取 text.xml 文件中的以下几行:

in order to get only the follwoing lines in the text.xml file:

 <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'); 

推荐答案

好的,关于我之前的评论,我现在找到了解决方案.

Okay, regarding my previous comment, I now found a solution.

似乎 toFile 绕过了 $skipXMLDeclarationtoString 没有.所以以下工作:

It seems toFile bypasses $skipXMLDeclaration whereas toString doesn't. So the following works:

$XML::LibXML::skipXMLDeclaration = 1;

my $doc  = XML::LibXML::Document->new;
# create your document

print $doc->toString;

(非常小的)缺点是您必须自己编写文件.

The (very small) downside is that you have to write the file yourself.

这篇关于perl + DOM + 使用 XML::LibXML + 如何从 XML 文件中删除 xml 版本标题?使用 DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆