Perl XML::LibXML replace text

眉间皱痕 提交于 2020-03-21 10:46:06

问题


Simple xml file cr.xml

<book_reviewers>
    <results>
        <reviewer>
            <name>Anne</name>
            <profession>Catfish wrangler</profession>
        </reviewer>
        <reviewer>
            <name>Bob</name>
            <profession>Beer taster</profession>
        </reviewer>
        <reviewer>
            <name>Charlie</name>
            <profession>Gardener</profession>
        </reviewer>
    </results>
</book_reviewers>

I want to loop through each reviewer and replace the name with a new one

I am not trying to use both these methods, but going by other posts in the form both of these should work but I keep getting errors:

Can't locate object method "setData"

Can't locate object method "removeChildNodes

Can't locate object method "appendText"

#!/usr/bin/perl -w

use strict;
use XML::LibXML;

my $critics_file = "cr.xml";
my $parser = new XML::LibXML;   


print "Couldn't retrieve review details\n" 
    unless my $book_reviews  = $parser->parse_file($reviews_file);


foreach my $critics ($critic_details->findnodes('/book_reviewers/results/reviewer')) {

    my $value = $critics->findvalue('name');    #returns the correct name
    $value->removeChildNodes();
    $value->appendText('new_name');

     ##ONLY EITHER THE ABOVE METHOD OR THE ONE BELOW - NOT BOTH

    my $node  = $critics->findnodes('.//name.text()');#returns the correct name
    $node->setData('new_name');


}

Can anyone see where I am going wrong?

Cheers


回答1:


Trying to get you into TIMTOWTDI, here is another example with more compact code:

You can find all the <name>…</name> elements directly, and you can iterate over them immediately, using the 'default' variable $_.

use strict;
use warnings;

use utf8;

use XML::LibXML;

my $filename = "cr.xml";

my $parser = XML::LibXML->new();
my $critic_details = $parser->parse_file("$filename") or die;

my $new_name = "new_name";

# find ALL the <book_reviewers><results><reviewers><name> nodes
foreach ($critic_details
  ->findnodes("book_reviewers/results/reviewer/name")
) {
  $_->removeChildNodes();
  $_->appendText($new_name);
}

use XML::LibXML::PrettyPrint;
my $pretty = XML::LibXML::PrettyPrint->new(
  indent_string =>' ' x4,
  element       => {
    compact       => [qw| name profession |],
    }
  );
$pretty->pretty_print($critic_details);

print $critic_details->toString;

__END__

enjoy!




回答2:


The problem is that this call

my $value = $critics->findvalue('name')

returns the string content of the name element, and you are trying to treat it as an XML::LibXML::Element object.

Just change the method call to

my ($value) = $critics->findnodes('name')

and all should be well.

Here is a working version of your program.

#!/usr/bin/perl

use strict;
use warnings;

use XML::LibXML;

my $reviews_file = 'cr.xml';
my $parser = XML::LibXML->new;

my $doc = $parser->parse_file($reviews_file) or die "Couldn't parse book reviewers\n";

for my $reviewer ($doc->findnodes('/book_reviewers/results/reviewer')) {
  my ($name) = $reviewer->findnodes('name');
  $name->removeChildNodes;
  $name->appendText('new_name');  
}

print $doc->toString;

output

<?xml version="1.0"?>
<book_reviewers>
    <results>
        <reviewer>
            <name>new_name</name>
            <profession>Catfish wrangler</profession>
        </reviewer>
        <reviewer>
            <name>new_name</name>
            <profession>Beer taster</profession>
        </reviewer>
        <reviewer>
            <name>new_name</name>
            <profession>Gardener</profession>
        </reviewer>
    </results>
</book_reviewers>



回答3:


Solution using XML::LibXML

The following directly accesses the Text Node to simply and easily change the enclosed text:

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

use XML::LibXML;

my $doc = XML::LibXML->load_xml( IO => \*DATA );

for my $text ( $doc->findnodes('//reviewer/name/text()') ) {
    $text->setData('new_name');
}

print $doc->toString;

__DATA__
<book_reviewers>
    <results>
        <reviewer>
            <name>Anne</name>
            <profession>Catfish wrangler</profession>
        </reviewer>
        <reviewer>
            <name>Bob</name>
            <profession>Beer taster</profession>
        </reviewer>
        <reviewer>
            <name>Charlie</name>
            <profession>Gardener</profession>
        </reviewer>
    </results>
</book_reviewers>

Solution using XML::Twig

Takes the name of each reviewer and just reverses them:

use strict;
use warnings;

use XML::Twig;

my $t= XML::Twig->new( 
    twig_handlers => {
        q{//reviewer/name} => sub { $_->set_text(scalar reverse $_->text()) },
    },
    pretty_print => 'indented',
);
$t->parse( do { local $/; <DATA> } );
$t->print;

__DATA__
<book_reviewers>
    <results>
        <reviewer>
            <name>Anne</name>
            <profession>Catfish wrangler</profession>
        </reviewer>
        <reviewer>
            <name>Bob</name>
            <profession>Beer taster</profession>
        </reviewer>
        <reviewer>
            <name>Charlie</name>
            <profession>Gardener</profession>
        </reviewer>
    </results>
</book_reviewers>

Outputs:

<book_reviewers>
  <results>
    <reviewer>
      <name>ennA</name>
      <profession>Catfish wrangler</profession>
    </reviewer>
    <reviewer>
      <name>boB</name>
      <profession>Beer taster</profession>
    </reviewer>
    <reviewer>
      <name>eilrahC</name>
      <profession>Gardener</profession>
    </reviewer>
  </results>
</book_reviewers>



回答4:


my $value = $critics->findvalue('name');    #returns the correct name
$value->removeChildNodes();
$value->appendText('new_name');

$value is a simple scalar that holds the text of the found xml-element <name>…</name>. You can not call any of those methods on a 'string', can you?



来源:https://stackoverflow.com/questions/23733255/perl-xmllibxml-replace-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!