我有一个
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);
并阅读文档,当然:)