[英]Perl parse xml with XML::LibXML dynamic node
I have this xml file, I need to get the categories, properties and test-case, I successfully done it using XML::LibXML and findnodes The problem is the sometimes the structure is different so there may be some more test-suite & results nodes then the node in the findnodes is not correct. 我有这个xml文件,我需要获取类别,属性和测试用例,我使用XML :: LibXML和findnodes成功完成了问题是有时结构不同,所以可能会有更多测试套件和结果节点,则findnodes中的节点不正确。
So what is the best way to handle it ? 那么最好的处理方法是什么? Not sure how to search for type="Fixture ( which is the node that has the info I need ) if I don't know the correct base starting node. 如果我不知道正确的基本起始节点,则不确定如何搜索type =“ Fixture(具有所需信息的节点)。
<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>
Take a look at XPath Examples
for some quick tips on how to create xpaths. 查看XPath Examples
获取有关如何创建xpath的一些快速提示。
In order to specify that you want a specific attribute, don't forget the @
symbol: //test-suite[@type="Fixture"]
. 为了指定您想要特定的属性,请不要忘记@
符号: //test-suite[@type="Fixture"]
。 Also, your current XML is missing a closing quote after "Fixture
which I'm going to assume is a copy/paste error. 另外,您当前的XML缺少"Fixture
我要假定的复制/粘贴错误"Fixture
之后的"Fixture
引号。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.