3

我有一个 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);
4

1 回答 1

3

由于没有文本,以下内容无法找到任何内容(设置$owner2undef) :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");

它确实 modify $doc2,但您也需要添加以下代码进行修改build.xml

$doc2->toFile('build.xml');
于 2013-09-10T13:17:18.213 回答