0

次の形式のXMLファイルがあります。

<Principal ID="122" >
<Status Fees="${Fees}"/>
</Principal>
<Principal ID="123" >
<Status Fees="${Fees}"/>
</Principal>
<Principal ID="124" >
<Status Fees="${Fees}"/>
</Principal>
<Principal ID="125" >
<Status Fees="${Fees}"/>
</Principal>

${Fees}Fees1、Fees2、Fees3などで変数を更新する必要があります。365以上のレコードがあります。誰かがPerlまたはPythonを介してこれを行う方法を教えてもらえますか?

4

2 に答える 2

0

xml操作に正規表現を使用するのは良い考えではありませんが、この場合はとにかくそれを使用します:

Perlスクリプト:

my $xml = join (/\n/, <main::DATA>);
my @fees = (1, 2, 3, 4);

$xml =~ s/(<Status Fees=")\${Fees}("\/>)/{$1 . shift(@fees) . $2}/ige;

print "$xml";

__DATA__
<Principal ID="122" > 
<Status Fees="${Fees}"/> 
</Principal> 
<Principal ID="123" > 
<Status Fees="${Fees}"/> 
</Principal> 
<Principal ID="124" > 
<Status Fees="${Fees}"/> 
</Principal> 
<Principal ID="125" > 
<Status Fees="${Fees}"/> 
</Principal>

出力:

<Principal ID="122" > 
<Status Fees="1"/> 
</Principal> 
<Principal ID="123" > 
<Status Fees="2"/> 
</Principal> 
<Principal ID="124" > 
<Status Fees="3"/> 
</Principal> 
<Principal ID="125" > 
<Status Fees="4"/> 
</Principal>

ここでそれを見てテストしてください


${Fees}変数の実際の値に置き換え$Fees、この変数の名前を変更する場合は、次のようにします。

Perlスクリプト:

my $xml = join (/\n/, <main::DATA>);
my ($fee1, $fee2, $fee3, $fee4) = (1, 2, 3, 4);

$xml =~ s/(<Status Fees=")\${(.*?)}("\/>)/{$1 . eval('$'.$2) . $3}/ige;

print "$xml";

__DATA__
<Principal ID="122" > 
<Status Fees="${fee1}"/> 
</Principal> 
<Principal ID="123" > 
<Status Fees="${fee2}"/> 
</Principal> 
<Principal ID="124" > 
<Status Fees="${fee3}"/> 
</Principal> 
<Principal ID="125" > 
<Status Fees="${fee4}"/> 
</Principal>

出力:

<Principal ID="122" > 
<Status Fees="1"/> 
</Principal> 
<Principal ID="123" > 
<Status Fees="2"/> 
</Principal> 
<Principal ID="124" > 
<Status Fees="3"/> 
</Principal> 
<Principal ID="125" > 
<Status Fees="4"/> 
</Principal>

ここでそれを見てテストしてください

于 2012-06-22T12:08:16.393 に答える
0

短いPythonバージョン。非常に非効率的ですが、365エントリを超えると即座に実行されます。

xml = open("INPUT.XML","rb").read()
count = 1
while "${Fees}" in xml:
    xml = xml.replace("${Fees}", "Fees%d"%count, 1)
    count += 1
open("OUTPUT.XML","wb").write(xml)
于 2012-06-22T12:30:02.683 に答える