4

XML::LibXML で値を更新すると、最初の 2 行が削除されます。1 つの更新された値を除いて、xml をそのまま保持したい。

私の元のxmlは次のとおりです。

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
 <property>
   <name>test.name</name>
   <value>help</value>
   <description>xml issue</description>
 </property>

....

コードは次のとおりです。

my $parser =XML::LibXML->new();
my $tree   =$parser->parse_file($file) or die $!;
my $root   =$tree->getDocumentElement;

my $searchPath="/configuration/property[name=\"$name\"]/value/text()";
my ($val)=$root->findnodes($searchPath);

$val->setData($new_val);
open (UPDXML, "> $file") or die "ERROR: Failed to write into $file...";
 print UPDXML $root->toString(1);
close (UPDXML);

print UPDXML $root->toStringC14N(1) を試しましたが、役に立ちません...

4

3 に答える 3

5

daxim と rpg の両方の回答がそれを行いますが、強調するために、ファイル全体を取得する$tree->toString()代わりに使用します。$root->toString()

于 2012-02-09T09:10:26.927 に答える
3

toFileXML::LibXML::Documentを参照してください。使用しているソフトウェアのドキュメントを読んでください。

use strictures;
use XML::LibXML qw();
my $file    = 'fnord.xml';
my $name    = 'test.name';
my $new_val = 'foo';

my $parser  = XML::LibXML->new;
my $tree    = $parser->parse_file($file);
my $root    = $tree->getDocumentElement;

my $searchPath  = "/configuration/property[name='$name']/value/text()";
my ($val)       = $root->findnodes($searchPath);

$val->setData($new_val);
$tree->toFile($file);

ファイル ルーチンは自動的に例外を発生させます。従来の Perl エラー チェックは必要ありません。

于 2012-02-09T07:38:38.727 に答える
1

これを試してみてください

$tree->toFile ($file);
于 2012-02-09T07:34:58.847 に答える