PerlXML::XPath向文档中添加一堆垃圾

PerlXML::XPath向文档中添加一堆垃圾,xml,perl,xpath,Xml,Perl,Xpath,我想通过XPATH更新web.xml。我注意到所需的元素已正确修改,但文档开头添加了一堆垃圾。我注意到,即使我不修改任何元素,只是解析和打印,我也会得到这些垃圾 守则: require Cwd; use File::Temp qw/ tempfile tempdir/; use lib 'menu/perl-modules/lib/site_perl'; use XML::XPath; use XML::XPath::NodeSet; #use strict; $file = "/tmp/we

我想通过XPATH更新web.xml。我注意到所需的元素已正确修改,但文档开头添加了一堆垃圾。我注意到,即使我不修改任何元素,只是解析和打印,我也会得到这些垃圾

守则:

require Cwd;
use File::Temp qw/ tempfile tempdir/;
use lib 'menu/perl-modules/lib/site_perl';
use XML::XPath;
use XML::XPath::NodeSet;
#use strict;

$file = "/tmp/web.xml";
my $xp   = XML::XPath->new( filename => $file );
my $root = $xp->find('/')->get_nodelist;
#$xp->setNodeText( $xpath, $newValue );

open( XPATH_FILE, "> $file" );
foreach my $nodes ( $xp->find('/')->get_nodelist ) {
  print XPATH_FILE $nodes->toString;
}
close(XPATH_FILE);
输入文件:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
   <filter>
      <filter-name>LocaleFilter</filter-name>
      ....
</web-app>

本地过滤器
....
输出:文档开头大约700行注释,看起来像是引用的dtd或其他东西的某种扩展。为了便于阅读,我只列出了前几行:

<!--
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

Copyright 2000-2007 Sun Microsystems, Inc. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License").  You
may not use this file except in compliance with the License. You can obtain
a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
Sun designates this particular file as subject to the "Classpath" exception
as provided by Sun in the GPL Version 2 section of the License file that
accompanied this code.  If applicable, add the following below the License
Header, with the fields enclosed by brackets [] replaced by your own
identifying information: "Portions Copyrighted [year]
[name of copyright owner]"

Contributor(s):

If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license."  If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above.  However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.
--><!--
This is the XML DTD for the Servlet 2.3 deployment descriptor.

我不明白为什么这个模块会考虑所有链接的DTD文档,因为据我所知,它没有进行有效性检查

此外,虽然该模块允许更改和添加文档的节点,但没有明显的方法删除节点

但是,要排除的注释是根节点的子节点,因此可以通过在根节点的唯一元素子节点上重新根化文档来有效地删除它们

此代码演示

use strict;
use warnings;
use autodie;
use 5.010;

use XML::XPath;

my $xp   = XML::XPath->new( ioref => *DATA );
my ($new_root) = $xp->findnodes('/*');

print $new_root->toString, "\n";

__DATA__
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <filter>
    <filter-name>LocaleFilter</filter-name>
  </filter>
</web-app>
使用严格;
使用警告;
使用自动模具;
使用5.010;
使用XML::XPath;
我的$xp=XML::XPath->new(ioref=>*数据);
my($new_root)=$xp->findnodes('/*');
打印$new\u root->toString,“\n”;
__资料__
本地过滤器
输出

<web-app>
  <filter>
    <filter-name>LocaleFilter</filter-name>
  </filter>
</web-app>

本地过滤器

使用XML::LibXML不会有这样的问题,它有一个类似的接口。谢谢,但如果可能的话,我宁愿避免使用它。我不控制系统上安装的软件包。请不要注释掉
使用strict
。在编写的每个Perl程序的顶部,必须始终
使用strict
使用warnings
。在许多方面,添加
使用strict
,然后注释掉它比一开始不使用它更糟糕。您应该始终测试
打开是否成功。如果愿意,您可以
使用autodie
,它会隐式检查大多数IO操作,而无需编写代码。我们可以看看输入
web.xml
文件吗?@Borodin,他提供了相关的块。DOCTYPE中提到的DTD正在内联。谢谢,这确实有帮助。因为我仍然需要在输出中使用该标题,所以我可以手动插入它。理想的解决方案是保留DOCTYPE头,但我可以接受这个。无论如何,您都必须这样做,因为原始案例的输出中没有包含
DOCTYPE
语句(奇怪的是,只是一堆注释),而且似乎无法说服模块包含它。我非常鼓励您安装它,因为它可能是最受推崇的XML处理模块。我也喜欢