PHP Generate XML file - SimpleXML, DOM, Reader/Writer Method

Generate XML file dynamically using PHP three different method,

Following table explain which are the good method to follow the standard and which are not.

Operation DOM XML Reader/Writer SimpleXML
Read
Poorly support
Write
Poorly support
Manipulate
Poorly support

Poorly support
Full Control
Namespace Support
Poorly support
XPATH Support
Validate

Generate XML file in PHP SimpleXML, DOM, Reader/Writer Method

DOM (Document Object Model) XML Method

DOM Advantages

  • Very fast for small document, load entire document into memory.

  • Simple PHP DOM interface.

  • Access anything in tree, traversed in multitude of way.

  • XML parsing library, libXML2 use for a DOM.

DOM Disadvantages

  • Load entire document into memory.

  • Data access only after completely document parsed.

  • Support PHP 5+ or later version.

Create XML using DOM

<?php
  $dom = new DOMDocument('1.0','UTF-8');
  $dom->formatOutput = true;

  $root = $dom->createElement('student');
  $dom->appendChild($root);

  $result = $dom->createElement('result');
  $root->appendChild($result);

  $result->setAttribute('id', 1);
  $result->appendChild( $dom->createElement('name', 'Opal Kole') );
  $result->appendChild( $dom->createElement('sgpa', '8.1') );
  $result->appendChild( $dom->createElement('cgpa', '8.4') );

  echo '<xmp>'. $dom->saveXML() .'</xmp>';
  $dom->save('result.xml') or die('XML Create Error');
?>

Result Output

<?xml version="1.0" encoding="UTF-8"?>
<student>
  <result id="1">
    <name>Opal Kole</name>
    <sgpa>8.1</sgpa>
    <cgpa>8.4</cgpa>
  </result>
</student>

Manipulate XML using DOM

Manipulate above created XML file.

<?php
  $dom = new DOMDocument();
  $dom->formatOutput = true;

  $dom->load('result.xml', LIBXML_NOBLANKS);

  $root = $dom->documentElement;
  $newresult = $root->appendChild( $dom->createElement('result') );

  $newresult->setAttribute('id', 2);
  $newresult->appendChild( $dom->createElement('name','Max Miller') );
  $newresult->appendChild( $dom->createElement('sgpa','8.7') );
  $newresult->appendChild( $dom->createElement('cgpa','8.2') );

  echo ''. $dom->saveXML() .'';
  $dom->save('new_result.xml') or die('XML Manipulate Error');
?>

Result Output

result.xml
<?xml version="1.0" encoding="UTF-8"?>
<student>
  <result id="1">
    <name>Opal Kole</name>
    <sgpa>8.1</sgpa>
    <cgpa>8.4</cgpa>
  </result>
  <result id="2">
    <name>Max Miller</name>
    <sgpa>8.7</sgpa>
    <cgpa>8.2</cgpa>
  </result>
</student>

Read XML using DOM

<?php
  $dom = new DOMDocument();
  $dom->load('new_result.xml');

  $root = $dom->documentElement;
  echo "This document is store " . $root->tagName . " Result Information" . "
"; $results = $root->getElementsByTagName( 'result' ); foreach( $results as $result){ echo 'Result Id: ' . $result->getAttribute('id'); foreach( $result->getElementsByTagName('name') as $name ){ echo ' Student ' . $name->nodeValue; } foreach( $result->getElementsByTagName('sgpa') as $sgpa ){ echo ' SGPA ' . $sgpa->nodeValue; } foreach( $result->getElementsByTagName('cgpa') as $cgpa ){ echo ' CGPA ' . $cgpa->nodeValue . "
"; } } ?>

Result Output

new_result.xml
This document is store student Result Information
Result Id: 1 Student Opal Kole SGPA 8.1 CGPA 8.4
Result Id: 2 Student Max Miller SGPA 8.7 CGPA 8.2

XML Reader/Writer Method

XML Reader/Writer Advantages

  • Uses little memory for reading and writing.

  • Allowing for accessing immediately.

  • Support PHP4+ and PHP5+.

XML Reader/Writer Disadvantages

  • Reading data only sequential.

  • All method are slow for accessing data.

XML Reader Example

<?php
  $reader = new XMLReader();
  $reader->open('new_result.xml');

  while( $reader->read() ){
    if($reader->nodeType !== XMLReader::ELEMENT){
      continue;
    }
    if($reader->localName === 'result'){
      echo 'Result Id: ' . $reader->getAttribute('id');
    }
    if($reader->localName === 'name'){
      echo ' Student ' . $reader->readString();
    }
    if($reader->localName === 'sgpa'){
      echo ' SGPA ' . $reader->readString();
    }
    if($reader->localName === 'cgpa'){
      echo ' CGPA ' . $reader->readString() . "
";; } } ?>

Result Output

Result Id: 1 Student Opal Kole SGPA 8.1 CGPA 8.4
Result Id: 2 Student Max Miller SGPA 8.7 CGPA 8.2

XML Writer Example

<?php
  $writer = new XMLWriter();
  $writer->openUri('result.xml');

  $writer->setIndentString('  ');
  $writer->setIndent(true);

  $writer->startDocument( '1.0', 'UTF-8' );
  $writer->startElement('student');

  $writer->startElement('result');
  $writer->writeAttribute('id', '1');

  $writer->writeElement('name', 'Opal Kole');
  $writer->writeElement('sgpa', '8.1');
  $writer->writeElement('cgpa', '8.4');
  $writer->endElement();   /* </result> */

  $writer->flush();
  $writer->endElement();  /* </student> */
  $writer->endDocument();
?>

Result Output

result.xml
<?xml version="1.0" encoding="UTF-8"?>
<student>
  <result id="1">
    <name>Opal Kole</name>
    <sgpa>8.1</sgpa>
    <cgpa>8.4</cgpa>
  </result>
</student>

SimpleXML Reading/Writing

SimpleXML Writing

<?php
  $simple_xml = new SimpleXMLElement('<student></student>');

  $simple_xml->addChild('result');
  $simple_xml->result[0]->addAttribute('id', 1);

  $simple_xml->result[0]->addChild('name', 'Opal Kole');
  $simple_xml->result[0]->addChild('sgpa', '8.1');
  $simple_xml->result[0]->addChild('cgpa', '8.4');

  $simple_xml->asXML('simple_xml_create.xml');
?>

Result Output

simple_xml_create.xml
<?xml version="1.0"?>
<student><result id="1"><name>Opal Kole</name><sgpa>8.1</sgpa><cgpa>8.4</cgpa></result></student>

SimpleXML Reading

<?php
  $simple_xml = simplexml_load_file('simple_xml_create.xml');

  foreach( $simple_xml->result as $result){
    echo 'Result Id: ' . $result['id'] . 
      ' Student ' . $result->name .
      ' SGPA ' . $result->sgpa .
      ' CGPA ' . $result->cgpa . "
"; } ?>

Result Output

Result Id: 1 Student Opal Kole SGPA 8.1 CGPA 8.4