使用perl脚本更新xml数据库文件

使用perl脚本更新xml数据库文件,xml,perl,xml-parsing,Xml,Perl,Xml Parsing,我想用3个其他xml文件按时间顺序更新一个150mo数据库xml文件。有人给我举了一个perl脚本的快速示例,说明如何执行此操作,但我不知道如何让脚本打开基本文件和3个更新。使用@args或使用open函数直接打开文件。 他对我说,我不需要解析xml文件,因为更新包含大量的数据库条目,只需要将这些条目的行放入一个散列中,并将条目id作为散列键,然后按顺序读取所有文件,以便条目更新或生成一个新的条目,然后按键的数字顺序写出散列 #! /usr/bin/perl -CIOE use strict;

我想用3个其他xml文件按时间顺序更新一个150mo数据库xml文件。有人给我举了一个perl脚本的快速示例,说明如何执行此操作,但我不知道如何让脚本打开基本文件和3个更新。使用@args或使用open函数直接打开文件。 他对我说,我不需要解析xml文件,因为更新包含大量的数据库条目,只需要将这些条目的行放入一个散列中,并将条目id作为散列键,然后按顺序读取所有文件,以便条目更新或生成一个新的条目,然后按键的数字顺序写出散列

#! /usr/bin/perl -CIOE
use strict;

my %h = ();
my $head = '';
my $has_data = 0;

while (<>) {
   /<db_entry db_id="(\d+)">/ and do {
    my $entry = $_;
    my $id = $1;
    while (<>) {
      $entry .= $_;
      /<\/db_entry>/ and last;
    }
    $h{$id} = $entry;
    $has_data = 1;
    next;
  };
  if (! $has_data) {
    $head .= $_;
    next;
  }
  /\s*<timestamp/ and do {
    $head .= $_;
    next;
  };
}

my $count = scalar keys %h;
print $head;
foreach (sort { $a <=> $b } keys %h) {
  print $h{$_};
}
print qq|  <db_entry_count count="$count" />
</databank_export>
|;
#/usr/bin/perl-CIOE
严格使用;
我的%h=();
我的$head='';
my$has_data=0;
而(){
//做{
我的$entry=$\;
my$id=$1;
而(){
$entry.=$\uUX;
//最后,;
}
$h{$id}=$entry;
$has_data=1;
下一个
};
如果(!$has_数据){
$head.=$\;
下一个
}

/\我试图添加一些信息(注释),以便您更好地理解。请注意,perl中的注释是使用“#”编写的。 除了第一行你看到的带有
#
符号的东西是注释

例如,如果您看到下面的代码有
#Hash initialization
,这意味着您需要理解一条注释。我强烈建议您使用
Notepad++
编辑器来编写perl代码或理解下面的代码。您将获得一个清晰的视图,因为在Notepad++中注释是绿色的。希望这对您有所帮助

#! /usr/bin/perl -CIOE
use strict;

my %h = ();                     # Hash initialization
my $head = '';                  # Variable initialization
my $has_data = 0;               # Variable initialization (Numeric)

# Perl's diamond <> operator to read from files. 
# It acts like a readline() command

while (<>) {                                

    # "\d"  matches all numbers; it is the same as [0-9] 
    # "+" sign for many expressions (1 or more)

    /<db_entry db_id="(\d+)">/ and do {     
    my $entry = $_;             # "$_" is a default operator
    my $id = $1;                # $1 is match operator (successful match)
    while (<>) {                # Again read from file
      $entry .= $_;             # Updating "$entry" variable everytime
      /<\/db_entry>/ and last;
    }
    $h{$id} = $entry;           # Preparing variable formed here will be value with index
                                # "$h{$id}" is equivalent to some value like "007XBCF(2)"

    $has_data = 1;              # assign value 1 to variable "has_data" which was 0
    next;                       # iteration
  };
  if (! $has_data) {            # If "$has_data" not exist then if loop runs
    $head .= $_;
    next;
  }
  /\s*<timestamp/ and do {
    $head .= $_;
    next;
  };
}

my $count = scalar keys %h;    # "scalar" will give you count for arrays or hashes
print $head;

# <=> compare operator of perl and rest syntax is of perl sort
foreach (sort { $a <=> $b } keys %h) {
  print $h{$_};
}
print qq|  <db_entry_count count="$count" />
</databank_export>
|;
!/usr/bin/perl-CIOE
严格使用;
我的%h=();#哈希初始化
my$head='';#变量初始化
my$has_data=0;#变量初始化(数字)
#Perl的菱形运算符来读取文件。
#它的作用类似于readline()命令
而{
#“\d”匹配所有数字;与[0-9]相同
#多个表达式的“+”符号(1个或多个)
//还有{
my$entry=$\u;#“$\ u”是默认运算符
my$id=$1;#$1是匹配运算符(成功匹配)
而(){#再次从文件中读取
$entry.=$\u;#每次更新“$entry”变量
//最后,;
}
$h{$id}=$entry;#这里形成的变量将是带有索引的值
#“$h{$id}”相当于某些值,如“007XBCF(2)”
$has_data=1;#将值1赋给变量“has_data”,该变量为0
下一步;#迭代
};
如果(!$has_data){#如果“$has_data”不存在,那么如果循环运行
$head.=$\;
下一个
}

/\我试图添加一些信息(注释),以便您更好地理解。请注意,perl中的注释是使用“#”编写的。 除了第一行你看到的带有
#
符号的东西是注释

例如,如果您看到下面的代码有
#Hash initialization
,这意味着您需要理解一条注释。我强烈建议您使用
Notepad++
编辑器来编写perl代码或理解下面的代码。您将获得一个清晰的视图,因为在Notepad++中注释是绿色的。希望这对您有所帮助

#! /usr/bin/perl -CIOE
use strict;

my %h = ();                     # Hash initialization
my $head = '';                  # Variable initialization
my $has_data = 0;               # Variable initialization (Numeric)

# Perl's diamond <> operator to read from files. 
# It acts like a readline() command

while (<>) {                                

    # "\d"  matches all numbers; it is the same as [0-9] 
    # "+" sign for many expressions (1 or more)

    /<db_entry db_id="(\d+)">/ and do {     
    my $entry = $_;             # "$_" is a default operator
    my $id = $1;                # $1 is match operator (successful match)
    while (<>) {                # Again read from file
      $entry .= $_;             # Updating "$entry" variable everytime
      /<\/db_entry>/ and last;
    }
    $h{$id} = $entry;           # Preparing variable formed here will be value with index
                                # "$h{$id}" is equivalent to some value like "007XBCF(2)"

    $has_data = 1;              # assign value 1 to variable "has_data" which was 0
    next;                       # iteration
  };
  if (! $has_data) {            # If "$has_data" not exist then if loop runs
    $head .= $_;
    next;
  }
  /\s*<timestamp/ and do {
    $head .= $_;
    next;
  };
}

my $count = scalar keys %h;    # "scalar" will give you count for arrays or hashes
print $head;

# <=> compare operator of perl and rest syntax is of perl sort
foreach (sort { $a <=> $b } keys %h) {
  print $h{$_};
}
print qq|  <db_entry_count count="$count" />
</databank_export>
|;
!/usr/bin/perl-CIOE
严格使用;
我的%h=();#哈希初始化
my$head='';#变量初始化
my$has_data=0;#变量初始化(数字)
#Perl的菱形运算符来读取文件。
#它的作用类似于readline()命令
而{
#“\d”匹配所有数字;与[0-9]相同
#多个表达式的“+”符号(1个或多个)
//还有{
my$entry=$\u;#“$\ u”是默认运算符
my$id=$1;#$1是匹配运算符(成功匹配)
而(){#再次从文件中读取
$entry.=$\u;#每次更新“$entry”变量
//最后,;
}
$h{$id}=$entry;#这里形成的变量将是带有索引的值
#“$h{$id}”相当于某些值,如“007XBCF(2)”
$has_data=1;#将值1赋给变量“has_data”,该变量为0
下一步;#迭代
};
如果(!$has_data){#如果“$has_data”不存在,那么如果循环运行
$head.=$\;
下一个
}

/\s*您是否可以修改您的问题,因为您的问题不清楚我的问题是如何让脚本按顺序读取/处理基本文件和3个更新?没有打开()有人给我的这个脚本中的函数,我不懂Perl,因为我刚刚开始学习它。我甚至无法测试它,因此无法修改它以使其正常工作。我不知道将条目的行放入散列是否有效。你能修改你的问题吗,因为它不清楚你在问什么我的问题是如何获得用于读取/处理基本文件和3个顺序更新的脚本?没有打开()有人给我的这个脚本中的函数,我不懂Perl,因为我刚刚开始学习它。我甚至无法测试它,因此无法修改它以使其工作。我不知道将条目的行放入散列是否有效。非常感谢,虽然有点晚了。这个脚本看起来没有任何修改。我更好地理解代码呃,现在。没有问题,但你可以投票给确实在帮助中的人:-)非常感谢,虽然有点晚了。脚本看起来没有任何修改。我现在更了解代码。没有问题,但你可以投票给确实在帮助中的人:-)