在 perl 中使用嵌套标签进行 XML 解析 [英] XML Parsing with nested tags in perl

查看:29
本文介绍了在 perl 中使用嵌套标签进行 XML 解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析包含嵌套标签集合的 xml 文件.我正在尝试使用 perl XML::Simple API 进行解析,并且可以准确解析单个标签值,但无法解析嵌套标签值.

I am trying to parse the xml file which has collection of nested tags.I was trying with perl XML::Simple API to parse and individual tag values are parsed exactly but couldn able to parse the nested tag values .

<archetype>
    <original_language></original_language>
    <description></description>
    <archetype_id>
    <definition></definition>
    <ontology></ontology>
</archetype>

在定义部分包含项目详细信息

in the definition part contains the item details

示例

<definition>
.
.
<node_id>at0004</node_id>
<attributes xsi:type="C_SINGLE_ATTRIBUTE">
<rm_attribute_name>value</rm_attribute_name>
+<existence> </existence>
<children xsi:type="C_DV_QUANTITY">
    <rm_type_name>DV_QUANTITY</rm_type_name>
    +<occurrences></occurrences>
    <node_id/>
    +<property></property>
    <list>
    <magnitude>
        <lower_included>true</lower_included>
        <upper_included>false</upper_included>
        <lower_unbounded>false</lower_unbounded>
        <upper_unbounded>false</upper_unbounded>
        <lower>0.0</lower>
        <upper>1000.0</upper>
</magnitude>
<units>mm[Hg]</units>
</list>
</children>
</attributes>
.
.
</definition>

从上面的示例文件格式,我想过滤内容

From the above example file format i would like to filter the content like

node_id - > at0004
    magnitude -> lower -> 0.0
    magnitude -> higher -> 1000.0

请指导我过滤内容.

推荐答案

需要了解的参考资料:perlreftut, perlref, perldsc.

You need to learn about references: perlreftut, perlref, perldsc.

use strictures;
use XML::Simple qw(:strict);

my $root = XMLin(<<'XML', ForceArray => 0, KeyAttr => undef);
<definition>
.
.
<node_id>at0004</node_id>
<attributes xsi:type="C_SINGLE_ATTRIBUTE">
<rm_attribute_name>value</rm_attribute_name>
+<existence> </existence>
<children xsi:type="C_DV_QUANTITY">
    <rm_type_name>DV_QUANTITY</rm_type_name>
    +<occurrences></occurrences>
    <node_id/>
    +<property></property>
    <list>
    <magnitude>
        <lower_included>true</lower_included>
        <upper_included>false</upper_included>
        <lower_unbounded>false</lower_unbounded>
        <upper_unbounded>false</upper_unbounded>
        <lower>0.0</lower>
        <upper>1000.0</upper>
</magnitude>
<units>mm[Hg]</units>
</list>
</children>
</attributes>
.
.
</definition>
XML

my $m = $root->{attributes}{children}{list}{magnitude};
printf <<'TEMPLATE', $root->{node_id}, $m->{lower}, $m->{upper};
node_id -> %s
    magnitude -> lower -> %.1f
    magnitude -> higher -> %.1f
TEMPLATE

use Data::Dump::Streamer qw(Dump); Dump $root;

<小时>

输出:

node_id -> at0004
    magnitude -> lower -> 0.0
    magnitude -> higher -> 1000.0

$HASH1 = {
    attributes => {
        children => {
            content => [("\n    +") x 2],
            list    => {
                magnitude => {
                    lower           => '0.0',
                    lower_included  => 'true',
                    lower_unbounded => 'false',
                    upper           => '1000.0',
                    upper_included  => 'false',
                    upper_unbounded => 'false'
                },
                units => 'mm[Hg]'
            },
            node_id      => {},
            occurrences  => {},
            property     => {},
            rm_type_name => 'DV_QUANTITY',
            "xsi:type"   => 'C_DV_QUANTITY'
        },
        content           => "\n+",
        existence         => {},
        rm_attribute_name => 'value',
        "xsi:type"        => 'C_SINGLE_ATTRIBUTE'
    },
    content => [("\n.\n.\n") x 2],
    node_id => 'at0004'
};

这篇关于在 perl 中使用嵌套标签进行 XML 解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆