gpt4 book ai didi

perl - 如何使用Perl的XML::LibXML提取标记中的属性?

转载 作者:行者123 更新时间:2023-12-04 09:08:35 24 4
gpt4 key购买 nike

我有一个XML文件

<PARENT >
<TAG string1="asdf" string2="asdf" >
</TAG >
</PARENT>

我想在这里提取string2值。我也想将其设置为新值。

怎么做?

最佳答案

使用XPath表达式

use strict;                                                                                                                      
use warnings;

use XML::LibXML;
use Data::Dumper;

my $doc = XML::LibXML->new->parse_string(q{
<PARENT>
<TAG string1="asdf" string2="asdfd">
</TAG>
</PARENT>
});

my $xpath = '/PARENT/TAG/@string2';
# getting value of attribute:
print Dumper $doc->findvalue($xpath);
my ($attr) = $doc->findnodes($xpath);

# setting new value:
$attr->setValue('dfdsa');
print Dumper $doc->findvalue($xpath);

# do following if you need to get string representation of your XML structure
print Dumper $doc->toString(1);

并阅读文档,当然:)

关于perl - 如何使用Perl的XML::LibXML提取标记中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1240282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com