【发布时间】:2015-01-27 12:47:49
【问题描述】:
我正在尝试用一些类似的标签解析一个巨大的 xml 文件。目前我只能解析第一个标签和 first_child
这是一个xml的例子:
<?xml version="1.0" encoding="UTF-8"?>
<test version="1.0">
<parameters/>
<category name="z1" description="jobs currently running" count="30" timestamp="2010-01-16T14:24:31">
<jobs name="ZEI018CL" owner="A" type="auto" activityLevel="147" threadId="202" pid="20521" vmName="Subs@xx.xxx.xx.xxx:6102:xxx" cpuUsage="0"/>
<job name="ZUA002B" owner="A" type="auto" activityLevel="3375" threadId="194" pid="20521" vmName="Subs@xx.xxx.xx.xxx:6102:xxx" cpuUsage="0"/>
<job name="ZZZ855" owner="A" type="auto" activityLevel="0" threadId="107" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZKA019CL" owner="A" type="auto" activityLevel="0" threadId="105" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZIN41B" owner="A" type="auto" activityLevel="3" threadId="104" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZIN198CL" owner="A" type="auto" activityLevel="0" threadId="103" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZHO060" owner="A" type="auto" activityLevel="61" threadId="102" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZEI019CL" owner="A" type="auto" activityLevel="0" threadId="101" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZEI013CL" owner="A" type="auto" activityLevel="0" threadId="99" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZEI011CL" owner="A" type="auto" activityLevel="0" threadId="98" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZEC007CL" owner="A" type="auto" activityLevel="0" threadId="97" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/>
<job name="ZEC001B" owner="A" type="auto" activityLevel="2" threadId="96" pid="20457" vmName="Subs@xx.xxx.xx.xxx:6101:xxx" cpuUsage="0"/></category>
<category name="z3" description="Batchjobs" count="0" timestamp="2015-01-16T14:24:31"/>
<category name="z4" description="Interactivejobs jobs currently running in the system" count="498" timestamp="2015-01-16T14:24:31">
<job name="CAS" owner="PA" type="interactive" activityLevel="0" threadId="14624" pid="23771" vmName="Subs@xx.xxx.xx.xxx:6104:xxx" cpuUsage="0"/>
<job name="CR" owner="K" type="interactive" activityLevel="0" threadId="14586" pid="23771" vmName="Subs@xx.xxx.xx.xxx:6104:xxx" cpuUsage="0"/>
<job name="MM" owner="DU" type="interactive" activityLevel="0" threadId="14570" pid="23771" vmName="Subs@xx.xxx.xx.xxx:6104:xxx" cpuUsage="0"/>
<job name="ZZ" owner="D" type="interactive" activityLevel="0" threadId="14568" pid="23771" vmName="Subs@xx.xxx.xx.xxx:6104:xxx" cpuUsage="0"/></category>
<category name="services" description="The status" timestamp="2015-01-16T14:24:31">
<service name="1" description="test1" port-status="up" thread-status="up"/>
<service name="2" description="test2" port-status="up" thread-status="up"/>
<service name="3" description="test3" port-status="N/A" thread-status="up"/>
<service name="4" description="test4" port-status="up" thread-status="up"/></category></test>
对于第一行我
my $parser = XML::Twig->new();
$parser->parsefile($xml);
对于我使用的第一行
my $count = $parser->root->first_child('category')->att('count');
print $count;
下一行这个
my $service = $parser->root->first_child('category')->first_child('job')->att('name');
print $service;
但我不知道如何获取特定名称的端口状态,例如:
或者对于特定的工作名称,第二个标签中的类型。
你能帮帮我吗?
【问题讨论】:
-
您能重新格式化您的问题吗?似乎有什么被删除了。您想要具有特定属性值的元素的属性吗?您可以通过
children方法使用类似于 xpath 的语言。 -
我想解析文件并提取一些字段,例如服务标签中的端口状态,例如 name=1