有没有可能的方法将 XML::Twig 对象转换为 XML::XPath 对象?
我长期使用XML::XPath
来解析xml,现在情况已经到了修改属性值的时候了。
xml 在文件中可用,它与此非常相似。
<AppName>
<Action AllDay='1'StartRace='1'/>
<StartPoint AM_PM='AM' Hours='09' Mins='30'/>
<EndPoint AM_PM='PM' Hours='06' Mins='30'/>
</AppName>
我编写了一个 perl 脚本来更改 StartPoint 标记的小时数,
use strict;
use warnings;
use XML::Twig;
my $fileName = 'Conf.xml';
my $twig = XML::Twig->new(
twig_roots => { StartPoint => \&ReplaceAtriValue },
twig_print_outside_roots => 1, );
sub ReplaceAtriValue {
my ($twig, $startPoint) = @_;
if ( '09' eq $startPoint->att('Hours') )
{
$startPoint->set_att( Hours => '12');
}
$startPoint->print(); }
$twig->parsefile($fileName);
我知道如何成功地将 09 替换为 12,现在 $twig
具有 xml 对象。如果我将此对象 ($twig
) 发送到使用 XML::XPath
解析 xml 的 SetWindow.pm,则无法识别该对象。是否有任何可能的方法将 XML::Twig
对象转换为 XML::XPath
对象,以便 XML::XPath
解析$twig
对象。
实际上,我已经使用 XML::XPath
很长时间了,因此我必须将 XML::XPath
对象发送到我自己的所有 *.pm,例如 SetWindow.pm,位于同时我需要在发送给我自己的 PM 之前更改标准 xml 的属性值。
提前致谢。
非常感谢任何帮助!
飞行员
I'm using XML::XPath
for a long time to parse xml, now the situation has come to modify the attribute value.
xml is available in a file, it will be very similar to this.
<AppName>
<Action AllDay='1'StartRace='1'/>
<StartPoint AM_PM='AM' Hours='09' Mins='30'/>
<EndPoint AM_PM='PM' Hours='06' Mins='30'/>
</AppName>
I have written a perl script to change the Hours of StartPoint tag,
use strict;
use warnings;
use XML::Twig;
my $fileName = 'Conf.xml';
my $twig = XML::Twig->new(
twig_roots => { StartPoint => \&ReplaceAtriValue },
twig_print_outside_roots => 1, );
sub ReplaceAtriValue {
my ($twig, $startPoint) = @_;
if ( '09' eq $startPoint->att('Hours') )
{
$startPoint->set_att( Hours => '12');
}
$startPoint->print(); }
$twig->parsefile($fileName);
I have some how successfully replaced 09 as 12, now $twig
has the xml object. If I send this object ($twig
) to my SetWindow.pm which parse xml using XML::XPath
, did not recognize this object. Is there any possible way to convert XML::Twig
object to XML::XPath
object so that XML::XPath
will parse values in the $twig
object.
Actually I have using XML::XPath
for long back hence I have to send the XML::XPath
object to all my own *.pm like SetWindow.pm, at the same time i need to change the values of the attribute of the standard xml before sending to my own PMs.
Thanks in Advance.
Any help is much appreciated!
rndpilot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML::Xpath 解析 XML,因此您应该将其发送给它,无论是在文件中还是作为字符串。
现在,如果您想变得聪明,并且您想要的只是从树枝中提取信息,您可以使用 XML::Twig::XPath 而不是 XML::Twig (::Twig::XPath 是 XML:: 的一部分twig 分布),并且 twig 对象及其元素将为 XML::XPath 获得足够的 DOM 式方法,以便能够对它们运行查询。但我不确定这是否值得。
我能想到的替代方法:
getAttributeNodes
和setValue
的组合应该可以做到这一点,XML::Xpath parses XML, so that's what you should be sending to it, either in a file or as a string.
Now if you want to be clever, and all you want is to extract information from the twig, you could use XML::Twig::XPath instead of XML::Twig (::Twig::XPath is part of the XML::twig distribution), and the twig object and its elements will get enough DOM-ish methods for XML::XPath to be able to run queries on them. I am not sure it's worth it though.
Alternate methods I can think of:
getAttributeNodes
andsetValue
should do it,