2 Einträge, 1 Seite |
use XML::LibXML 1.58;
1
2
3
4
5
6
7
8
my $xc = XML::LibXML->new;
$self->{_element} = $xc->parse_string(<<"data"
<?xml version="1.0" encoding="UTF-8"?>
<xData>
<xID>28344</xID>
<mType>s</mType>
</xData>
data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sub setvalue {
my $self = shift;
my $path = shift;
my $value = shift;
my ($node, $myChild);
if ( $node = @{$self->{_element}->findnodes($path)}[0] ) {
$node->removeChildNodes();
$node->appendTextNode( $value );
}
return $self;
}
$myXmlObject->setvalue('//xID', '123');
1
2
3
4
5
6
7
8
9
@{$self->{_element}->findnodes($path)}[0]->toString eq '<xID>123</xID>';
$self->{_element}->toString eq
'<?xml version="1.0" encoding="UTF-8"?>
<xData>
<xID>28344</xID>
<mType>s</mType>
</xData>
';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use strict;
use warnings;
use XML::LibXML;
my $xc = XML::LibXML->new;
my $node = $xc->parse_string('<?xml version="1.0" encoding="UTF-8"?><xData><xID>28344</xID><mType>s</mType></xData>');
print $node->toString;
my $path = '//xID';
my $value = '123';
if (my $subnode = $node->findnodes($path)->[0]) {
$subnode->removeChildNodes();
$subnode->appendTextNode( $value );
}
print $node->toString;
2 Einträge, 1 Seite |