出于某种原因,当我将元素追加到xml文件时,它被写成一行,即没有格式化
原始xml:
<configuration>
<property>
<name>test1</name>
</property>
</configuration>
my $parser =XML::LibXML->new();
my $doc =$parser->parse_file($file) or die $!;
my $root =$doc->getDocumentElement;
my $searchPath="/configuration";
my ($val)=$root->findnodes($searchPath);
my $propTag=$doc->createElement("property");
$val->appendChild($propTag);
my $nameTag=$doc->createElement("name");
$nameTag->appendTextNode($name);
$propTag->appendChild($nameTag);
$doc->setDocumentElement($root);
$doc->toFile($file,1);
结果是:
<configuration>
<property>
<name>test1</name>
</property>
<property><name>test2</name></property></configuration>
而不是:
<configuration>
<property>
<name>test1</name>
</property>
<property>
<name>test2</name>
</property>
</configuration>
答案 0 :(得分:2)
您可以在Perl脚本的输出上运行xmllint --format
。