8

我有一个 XML 文件

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

我想在这里提取 string2 值..我还想将它设置为一个新值..

怎么做?

4

4 回答 4

17

使用 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);                             

并阅读文档,当然:)

于 2009-08-06T17:30:25.367 回答
8

您也可以使用 XML::Parser 来获取值。有关更多信息,请参阅XML::Parser 文档

#!/usr/local/bin/perl
use strict;
use warnings;


use XML::Parser;
use Data::Dumper;

my $attributes = {};

my $start_handler = sub
{
    my ( $expat, $elem, %attr ) = @_;
    if ($elem eq 'TAG')
    {
        $attributes->{$attr{'string1'}} = 'Found';
    }
};


my $p1 = new XML::Parser(
        Handlers => {
            Start => $start_handler
        }
);

$p1->parsefile('test.xml');

print Dumper($attributes);
于 2009-08-06T19:53:46.047 回答
2

我认为您最好从XML::Simple开始并先玩一点:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Simple;

my $xml = XMLin(\*DATA);

print $xml->{TAG}->{string2}, "\n";

$xml->{TAG}->{string2} = "asdf";

print XMLout( $xml, RootName => 'PARENT');

__DATA__
<PARENT>
<TAG string1="asdf" string2="value of string 2">
</TAG>
</PARENT>
于 2009-08-07T14:43:06.307 回答
0

感谢您的回复。我在“使用 LibXML2 进行配置文件处理”中找到了另一个答案,我发现它非常有用。

于 2009-08-10T11:03:55.880 回答