首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl使用XML::LibXML动态节点解析xml

Perl使用XML::LibXML动态节点解析xml
EN

Stack Overflow用户
提问于 2014-05-12 12:28:03
回答 1查看 174关注 0票数 0

我有这个xml文件,我需要获得类别、属性和测试用例,我使用XML::LibXML和findnode成功地完成了它,问题是有时结构不同,所以可能会有更多的测试套件&结果节点,然后findnode中的节点是不正确的。

那么,处理这个问题的最好方法是什么呢?如果我不知道正确的基本起始节点,那么不确定如何搜索type=“夹具”(它是有我需要的信息的节点)。

代码语言:javascript
复制
<test-A>
 <test-suite type="Project">
    <results>
        <test-suite type="Setup">
          <results>
            <test-suite type="Fixture>
              <categories>
                <category name="AAA" />
                <category name="BBB" />
              </categories>
              <properties>
                <property name="CCC" />
                <property name="DDD" />
              </properties>
              <results>
                <test-case name="EEE" />
                <test-case name="DDD" />
               </results>
            </test-suite>
          </results>
        </test-suite>
    </results>
  </test-suite>
</test-A>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-12 20:53:33

看看XPath Examples,了解一些关于如何创建xpath的快速提示。

为了指定您需要一个特定的属性,不要忘记@符号://test-suite[@type="Fixture"]。另外,当前的XML在"Fixture之后缺少一个结束引号,我将假设它是一个复制/粘贴错误。

代码语言:javascript
复制
use strict;
use warnings;

use XML::LibXML;

my $dom = XML::LibXML->load_xml({IO => \*DATA});

for my $node ($dom->findnodes('//test-suite[@type="Fixture"]')) {
    print $node, "\n";
}


__DATA__
<test-A>
  <test-suite type="Project">
    <results>
        <test-suite type="Setup">
          <results>
            <test-suite type="Fixture">
              <categories>
                <category name="AAA" />
                <category name="BBB" />
              </categories>
              <properties>
                <property name="CCC" />
                <property name="DDD" />
              </properties>
              <results>
                <test-case name="EEE" />
                <test-case name="DDD" />
               </results>
            </test-suite>
          </results>
        </test-suite>
    </results>
  </test-suite>
</test-A>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23609068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档