Clear XML document by tag and output in PHP

Description

The following code shows how to clear XML document by tag and output.

Example


<?php/*w  ww . ja va  2 s .  c o m*/
$xml = simplexml_load_file('test.xml');
foreach ($xml->book as $book) {
  unset($book['isbn13']);
  unset($book->publisher);
  unset($book->price);
  unset($book->description);
}
header ('Content-Type: text/xml');
echo $xml->asXML();
?>

The following code is for test.xml.


<?xml version='1.0' encoding='utf-8'?>
<inventory>/*from  w  w w .ja  v  a 2s .c o m*/
  <book isbn13='1'>
    <title>PHP</title>
  <author>Jack</author>
  <publisher>Pub 3</publisher>
  <description>PHP Book</description>
  <price>
    <paperback>$3.99</paperback>
    <ebook>$2.89</ebook>
  </price>
  </book>
  <book isbn13='9'>
    <title>XML</title>
  <author>Jane</author>
  <publisher>Pub 2</publisher>
  <description>XML Book</description>
  <price>
    <paperback>$4.99</paperback>
    <ebook>$3.99</ebook>
  </price>
  </book>
</inventory>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser