Perl XML::Twig 问题请教
我正在使用 Perl 中的 XML::Twig 库,但我不太确定如何(或者甚至是否)可以执行以下操作。我还没有完成代码,因为我什至不知道从哪里开始。我真的在寻找一些想法,我可以(希望)从那里开始......
所以我想使用 XML::Twig 来查找值“This_Is_My_Name”,它是标签“MyClass.Name”的子值。我认为我可以完成这部分,但希望得到指导。
然后我想获取“MyClass.Code”LinkValue 编号,在下面的示例中为“987654321”。
希望这是有道理的。所以我不知道如何以这种方式移动。
请帮忙:)
所以我的 XML 文档如下,
<Object Class="MyClass" Id="123456789">
<Property Name="MyClass.Link">
<LinkValue>
<Id>2468</Id>
</LinkValue>
</Property>
<Property Name="MyClass.Code">
<LinkValue>
<Id>987654321</Id>
</LinkValue>
</Property>
<Property Name="MyClass.Name">
<StringValue>This_Is_My_Name</StringValue>
</Property>
</Object>
Im playing around with XML::Twig library in Perl, and Im not quite sure how (or even if) I can do the following. I have no code done yet as I dont even know where to start. Im really after some ideas and I can (hopefully) go from there...
So I want to use XML::Twig to find the value "This_Is_My_Name" which is a child value of the tag "MyClass.Name". I think I can do this part, but guidance would be appreciated.
Then I want to get the "MyClass.Code" LinkValue number, which in the below example is "987654321".
Hope that makes sense. So Im not sure how to move around in such a fashion.
Please help :)
So my XML doc is as follows,
<Object Class="MyClass" Id="123456789">
<Property Name="MyClass.Link">
<LinkValue>
<Id>2468</Id>
</LinkValue>
</Property>
<Property Name="MyClass.Code">
<LinkValue>
<Id>987654321</Id>
</LinkValue>
</Property>
<Property Name="MyClass.Name">
<StringValue>This_Is_My_Name</StringValue>
</Property>
</Object>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 xpath 来提取这些值。
This_Is_My_Name
的 xpath 是/Object/Property[@Name="MyClass.Name"]/StringValue
,LinkValue
的 xpath 是/Object/Property[@Name="MyClass.Code"]/LinkValue/Id
。代码是:You can use xpaths to extract these values. The xpath for
This_Is_My_Name
is/Object/Property[@Name="MyClass.Name"]/StringValue
and that forLinkValue
is/Object/Property[@Name="MyClass.Code"]/LinkValue/Id
. The code would be:在这种情况下,我通常使用树遍历方法,而不是使用 XPath。这里用
first_elt
来查找属性,然后用field
(相当于first_child()->text
)来获取链接值。In this case, rather than using XPath, I usually use the tree-traversal methods. Here
first_elt
to find the property, thenfield
(which is equivalent tofirst_child()->text
) to get the link value.