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

unhi4e5o  于 2023-01-31  发布在  Perl
关注(0)|答案(1)|浏览(154)

我有一个XML文件(information.xml)。我必须从此XML文件中提取元素和属性值,并将这些元素和属性值插入到另一个XML文件(build.xml)中。我必须通过从information.xml文件中填充适当的元素值和标记来更改build.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中提取owner的值,打开build.xml,在build.xml中搜索owner标记,并将提取的值放在那里。
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);
doinxwow

doinxwow1#

由于owner没有文本,因此以下命令无法找到任何内容(将$owner2设置为undef):

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');

相关问题