perl中带嵌套标记的XML解析

perl中带嵌套标记的XML解析,perl,xml-parsing,Perl,Xml Parsing,我试图解析包含嵌套标记集合的xml文件。我试图使用perl xml::Simple API进行解析,单个标记值被精确解析,但无法解析嵌套标记值 <archetype> <original_language></original_language> <description></description> <archetype_id> <definition></definiti

我试图解析包含嵌套标记集合的xml文件。我试图使用perl xml::Simple API进行解析,单个标记值被精确解析,但无法解析嵌套标记值

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

请指导我筛选内容。

您需要了解参考资料:

这里有一个程序可以做到这一点,尽管我做了一些假设,你可能需要调整。我不知道
是否可以有多个节点属性对,所以我写了这个来处理多个节点属性对:

#!/Users/brian/bin/perls/perl5.14.2

use XML::Twig;
use Data::Dumper;

my $twig = XML::Twig->new(
    twig_handlers => {
        magnitude => sub {
            my $m = $_;
            my $hash = $m->simplify;
            my $node_id = $m->parent( 'attributes' )->prev_sibling( 'node_id' )->text;
            print "node -> $node_id\n",
                "\tmagnitude -> lower -> $hash->{lower} $units\n",
                "\tmagnitude -> higher -> $hash->{upper} $units\n";
            },
        },
    );

$twig->parse(*DATA);


__END__
<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>

<node_id>at0005</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>100.9</lower>
                <upper>998.7</upper>
            </magnitude>
            <units>mm[Hg]</units>
        </list>
    </children>
</attributes>

</definition>
#/用户/brian/bin/perls/perl5.14.2
使用XML::Twig;
使用数据::转储程序;
my$twig=XML::twig->new(
细枝处理程序=>{
震级=>sub{
我的$m=$\;
my$hash=$m->simplify;
我的$node\u id=$m->parent('attributes')->prev\u sibling('node\u id')->text;
打印“node->$node\u id\n”,
“\tmagnitude->lower->$hash->{lower}$units\n”,
“\tmagnitude->higher->$hash->{upper}$units\n”;
},
},
);
$twig->parse(*数据);
__结束__
at0004
价值
DVU数量
真的
假的
假的
假的
0
1000
毫米[汞柱]
at0005
价值
DVU数量
真的
假的
假的
假的
100.9
998.7
毫米[汞柱]

如果包含当前代码,可能会很有用。这样我们就可以指出你哪里出了问题,而不仅仅是给你一个完整的答案。
node_id - > at0004
    magnitude -> lower -> 0.0
    magnitude -> higher -> 1000.0
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'
};
#!/Users/brian/bin/perls/perl5.14.2

use XML::Twig;
use Data::Dumper;

my $twig = XML::Twig->new(
    twig_handlers => {
        magnitude => sub {
            my $m = $_;
            my $hash = $m->simplify;
            my $node_id = $m->parent( 'attributes' )->prev_sibling( 'node_id' )->text;
            print "node -> $node_id\n",
                "\tmagnitude -> lower -> $hash->{lower} $units\n",
                "\tmagnitude -> higher -> $hash->{upper} $units\n";
            },
        },
    );

$twig->parse(*DATA);


__END__
<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>

<node_id>at0005</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>100.9</lower>
                <upper>998.7</upper>
            </magnitude>
            <units>mm[Hg]</units>
        </list>
    </children>
</attributes>

</definition>