【问题标题】:XML parsing using Perl [duplicate]使用 Perl 解析 XML [重复]
【发布时间】:2012-05-05 22:39:11
【问题描述】:

可能重复:
XML Parsing with nested tags in perl

在下面的xml结构中。我需要在 perl 中解析大小值的下限值和上限值,并且需要解析子标签中的 type 属性

<definition>
    <attributes>
        <children>
            <attributes>
                <children xsi:type="C_COMPLEX_OBJECT" >
                    <attributes>
                        <children>
                            <attributes>
                                <children><list><magnitude><lower>100</lower><upper>200</upper></magnitude></list></children>
                                <children><list><magnitude><lower>200</lower><upper>400</upper></magnitude></list></children>
                                <children><list><magnitude><lower>400</lower><upper>750</upper></magnitude></list></children>
                                <children><list><magnitude><lower>250</lower><upper>500</upper></magnitude></list></children>
                                <children><list><magnitude><lower>350</lower><upper>1000</upper></magnitude></list></children>
                            </attributes>
                        </children>
                    </attributes>
                </children>
               <children></children>
            </attributes>
        </children>
    </attributes>
</definition>

【问题讨论】:

  • 您可以使用 xPath 和 XML 解析来检索需求信息。如果您需要代码,请告诉我。你可以考虑使用 XML-Twig 和 libxml 进行 xml 解析。
  • 你已经在上一篇文章中问过同样的问题,stackoverflow.com/questions/10295506/…
  • @Nikil 该问题的解决方案已解决,这是我们无法解决的另一个问题

标签: perl xml-parsing


【解决方案1】:

这是一个使用 LibXML 来解析这个 xml 并获取所需值的小代码。

use XML::LibXML;
my $parser = XML::LibXML->new();
my $xml = $parser->parse_file('test.xml');

my @lower = $xml->find('//lower');
my @upper = $xml->find('//upper');

my $type = $xml->find('//children/@type');

@lower、@upper、$type 是节点。要提取字符串,请调用 to_lietral 子。例如$type->to_literal。

【讨论】:

  • 感谢回复,所有子节点都有下节点和上节点。我们确定第 5 个子节点具有所需的下限值和上限值。上面的代码检索了所有值
  • @Thangaraj-如果这对您有帮助,请投票:)
猜你喜欢
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 2011-03-25
  • 1970-01-01
  • 2017-10-28
  • 2011-05-09
相关资源
最近更新 更多