通过 Perl 脚本更改 XML 文件内容

发布于 2024-10-04 17:11:20 字数 1316 浏览 8 评论 0原文

此线程是填充 XML 文件的 Perl 脚本

我要更改的文件是:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration start="earth">
    <country-list>
      <country name="japan">
        <description></description>
        <start>1900</start>
        <end/>
      </country>
      <country name="italy">
        <description></description>
        <start>1950</start>
        <end/>
      </country>
      <country name="korea">
        <description></description>
        <start>1800</start>
        <end/>
      </country>
    </country-list>
  </configuration>

我想在此列表中添加一个新国家/地区。

在上一个问题中,用于填充 XML 文件的 Perl 脚本

#Get the list of cities as a list, then push "Tokyo" to it.
push @{$doc->{countries}->{country}->{'japan-'}->{city}}, 'Tokyo';

建议添加一个新标签,但就我而言,不确定如何使用“push”。我无法映射到正确的标签。

This thread is in continuation of Perl script to populate an XML file.

The file I want to change is:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration start="earth">
    <country-list>
      <country name="japan">
        <description></description>
        <start>1900</start>
        <end/>
      </country>
      <country name="italy">
        <description></description>
        <start>1950</start>
        <end/>
      </country>
      <country name="korea">
        <description></description>
        <start>1800</start>
        <end/>
      </country>
    </country-list>
  </configuration>

I want to add a new country here in this list.

In previous question, Perl script to populate an XML file.

#Get the list of cities as a list, then push "Tokyo" to it.
push @{$doc->{countries}->{country}->{'japan-'}->{city}}, 'Tokyo';

This was suggested to add a new tag, but in my case not sure how exactly can I use "push". I am not able to map to the correct tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2024-10-11 17:11:20

我发现 XML::DOM 使用起来更简单。它可能有点冗长,但你可以很容易地理解它在做什么。

use XML::DOM;

#parse the file
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("test.xml");
my $root = $doc->getDocumentElement();

#get the country-list element
my $countryListElement = pop(@{$root->getElementsByTagName('country-list')}); 

#create a new country element
my $newCountryElement= $doc->createElement('country');
$newCountryElement->setAttribute("name","England");

my $descElement= $doc->createElement('description');
$newCountryElement->appendChild($descElement);

my $startElement= $doc->createElement('start');
my $startTextNode= $doc->createTextNode('1900');
$startElement->appendChild($startTextNode);
$newCountryElement->appendChild($startElement);

my $endElement= $doc->createElement('end');
$newCountryElement->appendChild($endElement);

#add the country to the country-list
$countryListElement->appendChild($newCountryElement);

#print it out
print $doc->toString;

#print to file
$doc->printToFile("out.xml");

I find XML::DOM a lot simpler to use. It may be a bit verbose, but you can easily understand what it is doing.

use XML::DOM;

#parse the file
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("test.xml");
my $root = $doc->getDocumentElement();

#get the country-list element
my $countryListElement = pop(@{$root->getElementsByTagName('country-list')}); 

#create a new country element
my $newCountryElement= $doc->createElement('country');
$newCountryElement->setAttribute("name","England");

my $descElement= $doc->createElement('description');
$newCountryElement->appendChild($descElement);

my $startElement= $doc->createElement('start');
my $startTextNode= $doc->createTextNode('1900');
$startElement->appendChild($startTextNode);
$newCountryElement->appendChild($startElement);

my $endElement= $doc->createElement('end');
$newCountryElement->appendChild($endElement);

#add the country to the country-list
$countryListElement->appendChild($newCountryElement);

#print it out
print $doc->toString;

#print to file
$doc->printToFile("out.xml");
成熟稳重的好男人 2024-10-11 17:11:20

你不能使用推送。推送用于将项目追加到数组(列表)中。从之前有人给你的“push”命令来看,国家/地区表示为散列,而不是列表,因此你需要类似

$doc->{countries)->{country}->{Transylvania} = { };

这是为“Transylvania”创建一个空哈希。您的系统可能需要其中存在某种结构。

You can't use push. Push is for appending an item to an array (a list). Judging by the "push" command somebody gave you before, countries are represented as a hash, not a list, so you need something like

$doc->{countries)->{country}->{Transylvania} = {};

That is creating an empty hash for 'Transylvania'. Your system may require there to be some structure in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文