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

时间:2009-08-06 17:21:47

标签: perl libxml2

我有一个XML文件

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

我想在这里提取string2值..而且我想将它设置为新值..

怎么做?

4 个答案:

答案 0 :(得分:16)

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

阅读文档,当然:)

答案 1 :(得分:8)

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

#!/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);

答案 2 :(得分: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>

答案 3 :(得分:0)

感谢您的回复。我在“Config file processing with LibXML2”中找到了另一个答案,我发现它非常有用。