鉴于这种过于简单的XML文件:为什么不填充我的对象属性?
<Foo>Bar</Foo>
而这种代码,提取了Foo
元素的值:
use XML::Rabbit;
use Data::Dump::Tree;
class RunInfo does XML::Rabbit::Node {
has $.foo is xpath("/Foo");
}
sub MAIN ($file!) {
my $xml = RunInfo.new(file => $file);
dump $xml;
put "-----------------------";
put "Foo is $xml.foo()";
}
你会看到,foo
值Nil
,即使输出显示为Foo is Bar
:
.RunInfo @0 ├ $.foo = Nil ├ $.context is rw = .XML::Document @1 │ ├ $.version = 1.0.Str │ ├ $.encoding = Nil │ ├ %.doctype = {0} @2 │ ├ $.root = .XML::Element @3 │ │ ├ $.name is rw = Foo.Str │ │ ├ @.nodes is rw = [1] @4 │ │ │ └ 0 = .XML::Text @5 │ │ │ ├ $.text = Bar.Str │ │ │ └ $.parent is rw = .XML::Element §3 │ │ ├ %.attribs is rw = {0} @7 │ │ ├ $.idattr is rw = id.Str │ │ └ $.parent is rw = .XML::Document §1 │ ├ $.filename = example.xml.Str │ └ $.parent is rw = Nil └ $.xpath is rw = .XML::XPath @9 ├ $.document = .XML::Document §1 └ %.registered-namespaces is rw = {0} @11 ----------------------- Foo is Bar
(声明:我遇到这种行为今天来到我的代码,所以我写了起来Q &样式。其他答案欢迎。)。
顺便提一下,这里有链接到XML::Rabbit和Data::Dump::Tree。