在 perl 中使用 xml::simple 引用 xml 对象的顶层
Referencing top level of xml object using xml::simple in perl
一直在用头撞墙试图弄清楚这个问题。只是试图将最终 link 将这些游戏文件的各种组件(例如 wiki)组合在一起的脚本放在一起。我敢肯定这是一个简单的问题,就是不做尊重,但我一辈子都弄不明白。
这是 XML 的示例:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<!-- ??????????????????????????????????????????????????????????????????????????????? -->
<!-- ?????????? Die Till Day 7 ?????????? Guns (Gun Parts) ????????????????????????? -->
<!-- ??????????????????????????????????????????????????????????????????????????????? -->
<item id="1" name="pistol">
<property name="Meshfile" value="Items/Weapons/Ranged/Pistol/PistolPrefab" />
<property name="Material" value="metal" />
<property name="HoldType" value="1" />
<property name="Stacknumber" value="1" />
<property name="RepairTools" value="repairKit,repairKit2ndGeneration,repairKit3rdGeneration" />
<property name="Degradation" value="175" param1="false" />
<property name="DegradationBreaksAfter" value="false" />
<property name="SoundJammed" value="Weapons/weapon_jam" />
<property name="Attachments" value="flashlight02" />
<property class="Parts">
<property name="Stock" value="pistol_grip" />
<property name="Receiver" value="pistol_receiver" />
<property name="Pump" value="pistol_parts" />
<property name="Barrel" value="pistol_barrel" />
</property>
<property class="Action0">
<property name="Class" value="Ranged" />
<property name="Delay" value="0.15" />
<property name="Range" value="120" />
<property name="DamageBlock" value="1" />
<property name="Magazine_size" value="15" />
<property name="Magazine_items" value="9mmBullet, incendiary9mmBullet" />
<property name="Reload_time" value="2" />
<property name="Bullet_icon" value="pistol" />
<property name="Sound_start" value="Weapons/Ranged/Pistol/pistol_fire" />
<property name="Sound_repeat" value="" />
<property name="Sound_end" value="" />
<property name="Sound_empty" value="Weapons/weapon_empty" />
<property name="Sound_reload" value="Weapons/Ranged/Pistol/pistol_reload" />
<property name="Particles_muzzle_fire" value="nozzleflash" />
<property name="Particles_muzzle_smoke" value="nozzlesmoke" />
<property name="DamageBonus.head" value="6" />
<property name="DamageBonus.glass" value="25" />
</property>
<property class="Action1">
<property name="Class" value="Zoom" />
<property name="Zoom_max_out" value="35" />
<property name="Zoom_max_in" value="35" />
</property>
<property name="LightSource" value="lightSource" />
<property name="ActivateObject" value="Attachments/flashlight/lightSource" />
<property name="AttachmentFlashlight" value="flashlight02" />
<property name="Group" value="Ammo/Weapons" />
<property class="Preview">
<property name="Zoom" value="14" />
<property name="Pos" value="0.1,-0.1" />
<property name="Rot" value="0,-45,0" />
</property>
</item>
</items>
这是实际的程序
#!/usr/local/bin/perl
use warnings;
#use strict;
use XML::Simple qw(:strict);
use Data::Dumper;
my $xml = new XML::Simple;
my $data = $xml->XMLin(undef, KeyAttr => { item => 'name' }, ForceArray => ['item', 'property', 'property']);
#my $data = XMLin ($xml, forcearray => [ qw (item property property) ], keyattr = +> [] );
#print Dumper($data);
#my @itemlist = @{$data->{name}};
#print Dumper( \@itemlist );
my $items = $data->{item};
for my $item_name (keys %$items) {
my $item = $items->{$item_name};
my $item_id = $item->{id};
my $props = $item->{property};
print "Name: " . $item_name . " ID: " . $item_id;
for my $prop (@$props)
{
my $prop_name = $prop->{name};
my $prop_value = $prop->{value};
print $prop_name ": " . $prop_value . "\n";
}
}
真的只是在寻找一些东西,比如我如何从 foreach 访问实际项目名称(在本例中为 pistol
)以及我如何找到 属性 或 属性 的一个 属性。我很确定我只是搞砸了什么是散列与数组或者没有正确设置项目但是我还没有找到任何足够接近我的 xml 数据的例子来弄清楚。
(Data Dumper 给了我这个)
$VAR1 = {
'item' => {
'pistol' => {
'id' => '1',
'property' => [
{
'value' => 'Items/Weapons/Ranged/Pistol/PistolPrefab',
'name' => 'Meshfile'
},
{
'value' => 'metal',
'name' => 'Material'
},
{
'value' => '1',
'name' => 'HoldType'
},
{
'value' => '1',
'name' => 'Stacknumber'
},
{
'value' => 'repairKit,repairKit2ndGeneration,repairKit3rdGeneration',
'name' => 'RepairTools'
},
{
'value' => '175',
'name' => 'Degradation',
'param1' => 'false'
},
etc
错误:
不再有错误!
my $items = $data->{item};
for my $item_name (keys %$items) {
my $item = $items->{$item_name};
my $item_id = $item->{id};
my $props = $item->{property};
for my $prop (@$props) {
my $prop_name = $prop->{name};
my $prop_value = $prop->{value};
...
}
}
您已经更新以显示某些属性如下所示:
{
'name' => '...'
'value' => '...',
}
有些看起来像
{
'class' => '...',
'property' => [ ... ],
}
但是你的代码只处理第一种。你可能想要像 if ($prop->{class})
.
这样的东西
print $prop_name ": " . $prop_value . "\n";
将 $prop_name
视为文件句柄。应该是
print $prop_name . ": " . $prop_value . "\n";
这并没有直接回答您的问题,但我希望展示另一种方法 - 我坚信 XML::Simple
... 不是。它使您的代码比需要的复杂得多。所以一些关于解析你的 XML:
的例子
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
#parse the file
my $twig = XML::Twig -> new();
$twig -> parsefile ( 'sample.xml' );
#iterate 'item' elements - note '//' means anywhere in document.
#you can use './' to denote only things under the current node.
foreach my $item ( $twig -> get_xpath ('//item') ) {
#item attributes:
print "ID: ", $item -> att('id'),"\n";
print "name: ", $item -> att('name'),"\n";
#subelement with a specific attribute name:
print "Material: ", $item -> get_xpath("./property[\@name='Material']", 0) -> att('value'),"\n";
#grab a nested property:
print "Action0 Range:";
#nb - errors on item 2, because it doesn't exist. This is illustrative.
print $item -> get_xpath(q{./property[@class='Action0']/property[@name='Range']},0) -> att('value'),"\n";
#iterate children:
print "Properties:\n";
#note - children only iterates beneath current node.
foreach my $property ( $item -> children('property') ) {
print $property -> att('name'),"\n" if defined $property->att('name');
}
}
一直在用头撞墙试图弄清楚这个问题。只是试图将最终 link 将这些游戏文件的各种组件(例如 wiki)组合在一起的脚本放在一起。我敢肯定这是一个简单的问题,就是不做尊重,但我一辈子都弄不明白。
这是 XML 的示例:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<!-- ??????????????????????????????????????????????????????????????????????????????? -->
<!-- ?????????? Die Till Day 7 ?????????? Guns (Gun Parts) ????????????????????????? -->
<!-- ??????????????????????????????????????????????????????????????????????????????? -->
<item id="1" name="pistol">
<property name="Meshfile" value="Items/Weapons/Ranged/Pistol/PistolPrefab" />
<property name="Material" value="metal" />
<property name="HoldType" value="1" />
<property name="Stacknumber" value="1" />
<property name="RepairTools" value="repairKit,repairKit2ndGeneration,repairKit3rdGeneration" />
<property name="Degradation" value="175" param1="false" />
<property name="DegradationBreaksAfter" value="false" />
<property name="SoundJammed" value="Weapons/weapon_jam" />
<property name="Attachments" value="flashlight02" />
<property class="Parts">
<property name="Stock" value="pistol_grip" />
<property name="Receiver" value="pistol_receiver" />
<property name="Pump" value="pistol_parts" />
<property name="Barrel" value="pistol_barrel" />
</property>
<property class="Action0">
<property name="Class" value="Ranged" />
<property name="Delay" value="0.15" />
<property name="Range" value="120" />
<property name="DamageBlock" value="1" />
<property name="Magazine_size" value="15" />
<property name="Magazine_items" value="9mmBullet, incendiary9mmBullet" />
<property name="Reload_time" value="2" />
<property name="Bullet_icon" value="pistol" />
<property name="Sound_start" value="Weapons/Ranged/Pistol/pistol_fire" />
<property name="Sound_repeat" value="" />
<property name="Sound_end" value="" />
<property name="Sound_empty" value="Weapons/weapon_empty" />
<property name="Sound_reload" value="Weapons/Ranged/Pistol/pistol_reload" />
<property name="Particles_muzzle_fire" value="nozzleflash" />
<property name="Particles_muzzle_smoke" value="nozzlesmoke" />
<property name="DamageBonus.head" value="6" />
<property name="DamageBonus.glass" value="25" />
</property>
<property class="Action1">
<property name="Class" value="Zoom" />
<property name="Zoom_max_out" value="35" />
<property name="Zoom_max_in" value="35" />
</property>
<property name="LightSource" value="lightSource" />
<property name="ActivateObject" value="Attachments/flashlight/lightSource" />
<property name="AttachmentFlashlight" value="flashlight02" />
<property name="Group" value="Ammo/Weapons" />
<property class="Preview">
<property name="Zoom" value="14" />
<property name="Pos" value="0.1,-0.1" />
<property name="Rot" value="0,-45,0" />
</property>
</item>
</items>
这是实际的程序
#!/usr/local/bin/perl
use warnings;
#use strict;
use XML::Simple qw(:strict);
use Data::Dumper;
my $xml = new XML::Simple;
my $data = $xml->XMLin(undef, KeyAttr => { item => 'name' }, ForceArray => ['item', 'property', 'property']);
#my $data = XMLin ($xml, forcearray => [ qw (item property property) ], keyattr = +> [] );
#print Dumper($data);
#my @itemlist = @{$data->{name}};
#print Dumper( \@itemlist );
my $items = $data->{item};
for my $item_name (keys %$items) {
my $item = $items->{$item_name};
my $item_id = $item->{id};
my $props = $item->{property};
print "Name: " . $item_name . " ID: " . $item_id;
for my $prop (@$props)
{
my $prop_name = $prop->{name};
my $prop_value = $prop->{value};
print $prop_name ": " . $prop_value . "\n";
}
}
真的只是在寻找一些东西,比如我如何从 foreach 访问实际项目名称(在本例中为 pistol
)以及我如何找到 属性 或 属性 的一个 属性。我很确定我只是搞砸了什么是散列与数组或者没有正确设置项目但是我还没有找到任何足够接近我的 xml 数据的例子来弄清楚。
(Data Dumper 给了我这个)
$VAR1 = {
'item' => {
'pistol' => {
'id' => '1',
'property' => [
{
'value' => 'Items/Weapons/Ranged/Pistol/PistolPrefab',
'name' => 'Meshfile'
},
{
'value' => 'metal',
'name' => 'Material'
},
{
'value' => '1',
'name' => 'HoldType'
},
{
'value' => '1',
'name' => 'Stacknumber'
},
{
'value' => 'repairKit,repairKit2ndGeneration,repairKit3rdGeneration',
'name' => 'RepairTools'
},
{
'value' => '175',
'name' => 'Degradation',
'param1' => 'false'
},
etc
错误:
不再有错误!
my $items = $data->{item};
for my $item_name (keys %$items) {
my $item = $items->{$item_name};
my $item_id = $item->{id};
my $props = $item->{property};
for my $prop (@$props) {
my $prop_name = $prop->{name};
my $prop_value = $prop->{value};
...
}
}
您已经更新以显示某些属性如下所示:
{
'name' => '...'
'value' => '...',
}
有些看起来像
{
'class' => '...',
'property' => [ ... ],
}
但是你的代码只处理第一种。你可能想要像 if ($prop->{class})
.
print $prop_name ": " . $prop_value . "\n";
将 $prop_name
视为文件句柄。应该是
print $prop_name . ": " . $prop_value . "\n";
这并没有直接回答您的问题,但我希望展示另一种方法 - 我坚信 XML::Simple
... 不是。它使您的代码比需要的复杂得多。所以一些关于解析你的 XML:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
#parse the file
my $twig = XML::Twig -> new();
$twig -> parsefile ( 'sample.xml' );
#iterate 'item' elements - note '//' means anywhere in document.
#you can use './' to denote only things under the current node.
foreach my $item ( $twig -> get_xpath ('//item') ) {
#item attributes:
print "ID: ", $item -> att('id'),"\n";
print "name: ", $item -> att('name'),"\n";
#subelement with a specific attribute name:
print "Material: ", $item -> get_xpath("./property[\@name='Material']", 0) -> att('value'),"\n";
#grab a nested property:
print "Action0 Range:";
#nb - errors on item 2, because it doesn't exist. This is illustrative.
print $item -> get_xpath(q{./property[@class='Action0']/property[@name='Range']},0) -> att('value'),"\n";
#iterate children:
print "Properties:\n";
#note - children only iterates beneath current node.
foreach my $property ( $item -> children('property') ) {
print $property -> att('name'),"\n" if defined $property->att('name');
}
}