1

出于某种原因,当我将元素附加到 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>
4

1 回答 1

2

您可以xmllint --format在 Perl 脚本的输出上运行。

于 2012-02-09T12:36:39.740 回答