有没有可能的方法将 XML::Twig 对象转换为 XML::XPath 对象?

发布于 2024-12-11 06:18:44 字数 1278 浏览 0 评论 0原文

我长期使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱&纷飞 2024-12-18 06:18:44

XML::Xpath 解析 XML,因此您应该将其发送给它,无论是在文件中还是作为字符串。

现在,如果您想变得聪明,并且您想要的只是从树枝中提取信息,您可以使用 XML::Twig::XPath 而不是 XML::Twig (::Twig::XPath 是 XML:: 的一部分twig 分布),并且 twig 对象及其元素将为 XML::XPath 获得足够的 DOM 式方法,以便能够对它们运行查询。但我不确定这是否值得。

我能想到的替代方法:

  • 为什么不直接使用 XML::XPath 更改属性? getAttributeNodessetValue 的组合应该可以做到这一点,
  • 用 XML::LibXML 替换 XML::XPath。界面非常相似(DOM + XPath),并且更好的模块
  • 使用 XML::Twig::XPath 在 XML::Twig 中获得完整的 XPath 支持

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:

  • why don't you change the attributes directly using XML::XPath? a combination of getAttributeNodes and setValue should do it,
  • replace XML::XPath by XML::LibXML. the interface is very similar (DOM + XPath) and it's a lot better module
  • use XML::Twig::XPath to get full XPath support in XML::Twig
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文