Set new version and encoding value for saved xml document in PHP

Description

The following code shows how to set new version and encoding value for saved xml document.

Example


<?php//from  ww  w  . ja  v a2s. c o  m
$xml = simplexml_load_file('test.xml');
$output = "<?xml version='1.0' encoding='utf-8'?>\n";
$output .= $xml->book[1]->asXML();
if (file_put_contents( 'book2_dec.xml', $output)) {
  echo 'XML saved';
} else {
  echo 'Could not save XML';
}

?>

The following code is for test.xml.


<?xml version='1.0' encoding='utf-8'?>
<inventory>/*from w w  w.j  a va 2 s .  c  o m*/
  <book isbn13='1'>
    <title>PHP</title>
  <author>Jack</author>
  <publisher>Publisher 1</publisher>
  <description>PHP Book</description>
  </book>
  <book isbn13='2'>
    <title>XML</title>
  <author>Jane</author>
  <publisher>Publisher 2</publisher>
  <description>XML Book</description>
  </book>
</inventory>




















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser