如何使用XML :: LibXML实际修改XML文件的值

时间:2013-09-10 07:10:03

标签: perl xml-parsing vmware xml-libxml

我有一个XML文件(information.xml)。我必须从此XML文件中提取元素和属性值,并将这些元素和属性值插入另一个XML文件(build.xml)。我必须通过填充build.xml文件中的相应元素值和标记来更改information.xml文件。

我必须使用XML :: LibXML才能这样做。我能够从information.xml中提取元素和属性值。但是,我无法在build.xml

中打开并填写这些值

示例:

information.xml

<info>
  <app version="10.5.10" long_name ="My Application">
    <name> MyApp </name>
    <owner>larry </owner>
    <description> This is my first application</description>
  </app>
</info>

build.xml

<build long_name="" version="">
  <section type="Appdesciption">
    <description> </description>
  </section>
  <section type="Appdetails">
    <app_name> </app_name>
    <owner></owner>
  </section>
</build>

现在,我的任务是从information.xml中提取所有者的价值,打开build.xml,在build.xml中搜索所有者标记,然后将提取的值放在那里。

Perl脚本如下所示:

#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $file1="/root/shubhra/myapp/information.xml";
my $file2="/root/shubhra/myapp/build.xml";

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($file1);
foreach my $line ($doc->findnodes('//info/app'))
{
    my $owner= $line->findnodes('./owner');  # 1st way
    print "\n",$owner->to_literal,"\n";

    my ($long_name) = $line->findvalue('./@long_name');  # 2nd way
    print "\n $long_name \n";

    my $version = $line->findnodes('@version');
    print "\n",$version->to_literal,"\n";
}

my $parser2 = XML::LibXML->new();
my $doc2 = $parser2->parse_file($file2);
foreach my $line2 ($doc2->findnodes('//build'))
{
    my ($owner2)= $line2->findnodes('./section/owner/text()');

    my ($version2)=$line2->findvalue('./@version');

    print "\n Build.xml already has version : $version2 \n";
    print "\n Build.xml already has owner :",$owner2->to_literal;

    $owner2->setData("Windows Application 2"); # Not changing build.xml
    $line2->setAttribute(q|version|,"60.60.60");  # Not changing build.xml 
    my $changedversion = $line2->getAttribute(q|version|); 
    #superficially changed but didn't changed build.xml content
    print "\n The changed version is : $changedversion";
}

build.xml看起来像:

<build long_name="" version="9.10.10">
<section type="Appdesciption">
<description> </description>
</section>
<section type="Appdetails">
<app_name> </app_name>
<owner>shubhra</owner>
</section>
</build>

my $doc3 = XML::LibXML->load_xml(location => $file2, no_blanks => 1);
my $xpath_expression = '/build/section/owner/text()';
my @nodes = $doc3->findnodes( $xpath_expression );
for my $node (@nodes) {
    my $content = $node->toString;
    $content = $owner;
    $node->setData($content);
}
$doc->toFile($file2 . '.new', 1);

1 个答案:

答案 0 :(得分:2)

以下无法找到任何内容(将$owner2设置为undef),因为owner没有文字:

my ($owner2) = $line2->findnodes('./section/owner/text()');

你想要

my ($owner2) = $line2->findnodes('./section/owner');

这需要改变

print "\n Build.xml already has owner :", $owner2->to_literal;

print "\n Build.xml already has owner :", $owner2->textContent;

$owner2->setData("Windows Application 2");

$owner2->removeChildNodes();
$owner2->appendText("Windows Application 2");

您暗示您希望以下内容不会更改build.xml,但它甚至不会提及build.xml

$line2->setAttribute(q|version|, "60.60.60");

它会修改$doc2,但您还需要添加以下代码来修改build.xml

$doc2->toFile('build.xml');