XML :: LibXML remove header when writing to xml

When I update a value using XML :: LibXML, the first two lines are deleted. I want to keep xml as is, except for one updated value.

My original xml:

<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>test.name</name> <value>help</value> <description>xml issue</description> </property> 

....

And the code:

 my $parser =XML::LibXML->new(); my $tree =$parser->parse_file($file) or die $!; my $root =$tree->getDocumentElement; my $searchPath="/configuration/property[name=\"$name\"]/value/text()"; my ($val)=$root->findnodes($searchPath); $val->setData($new_val); open (UPDXML, "> $file") or die "ERROR: Failed to write into $file..."; print UPDXML $root->toString(1); close (UPDXML); 

I tried with UPDXML printing $ root-> toStringC14N (1), but that does not help ...

+4
source share
3 answers

And the daxim and rpg answers do this, but emphasize - use $tree->toString() instead of $root->toString() to get the whole file.

+5
source

See toFile in XML :: LibXML :: Document . Check out the documentation for the software you are working with.

 use strictures; use XML::LibXML qw(); my $file = 'fnord.xml'; my $name = 'test.name'; my $new_val = 'foo'; my $parser = XML::LibXML->new; my $tree = $parser->parse_file($file); my $root = $tree->getDocumentElement; my $searchPath = "/configuration/property[name='$name']/value/text()"; my ($val) = $root->findnodes($searchPath); $val->setData($new_val); $tree->toFile($file); 

File routines automatically throw exceptions, do not require traditional Perl error checking.

+3
source

Please, try

 $tree->toFile ($file); 
+1
source

Source: https://habr.com/ru/post/1395550/


All Articles