Advertisement
imoda

PHP class to Create XML Documents

Aug 29th, 2011
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php
  2.  
  3.     class xmldoc {
  4.        
  5.         function build($name = 'index') {
  6.            
  7.             $dom = new DOMDocument("1.0");
  8.             $dom->formatOutput = true;
  9.             $root = $dom->createElement($name);
  10.             $dom->appendChild($root);
  11.            
  12.             return array($root, $dom);
  13.         }
  14.        
  15.         function add($xmlObject, $parent, $field, $value = false, $textNode = false) {
  16.            
  17.             $dom = $xmlObject[1];
  18.            
  19.             $field = $dom->createElement($field);
  20.             $parent->appendChild($field);
  21.            
  22.             if ($value !== false) {
  23.                
  24.                
  25.                 $text = ($textNode !== false ? $dom->createTextNode($value) : $dom->createCDATASection($value));
  26.                 $field->appendChild($text);
  27.             }
  28.            
  29.             return $field;
  30.         }
  31.        
  32.         function attr($xmlObject, $field, $name, $text) {
  33.            
  34.             $dom = $xmlObject[1];
  35.            
  36.             $name = $dom->createAttribute($name);
  37.             $field->appendChild($name);
  38.            
  39.             $value = $dom->createTextNode($text);
  40.             $name->appendChild($value);
  41.         }
  42.        
  43.         function save($xmlObject, $path, $save = true) {
  44.            
  45.             $dom = $xmlObject[1];
  46.            
  47.             echo $dom->saveXML();
  48.            
  49.             if ($save === true) {
  50.                
  51.                 echo "Wrote to {$path}, " . $dom->save($path) . " bytes.\n" . date('r', time()) . "\n";
  52.             }
  53.         }
  54.     }
  55.    
  56.     /* Example usage
  57.  
  58.     $xmlPath = '/path/to/doc/';
  59.  
  60.     $xmltool = new xmldoc();
  61.     $xmlObject = $xmltool->build('info');
  62.     $root = $xmlObject[0];
  63.    
  64.     $client = $xmltool->add($xmlObject, $root, 'client');
  65.     $xmltool->add($xmlObject, $client, 'name', 'John');
  66.     $xmltool->add($xmlObject, $client, 'age', 21, true);
  67.     $height = $xmltool->add($xmlObject, $client, 'height', '6.0');
  68.     $xmltool->attr($xmlObject, $height, 'unit', 'feet');
  69.    
  70.     $xmltool->save($xmlObject, $xmlPath . 'info.xml', false);
  71.    
  72.     Output:
  73.    
  74.     <?xml version="1.0"?>
  75.     <info>
  76.       <client>
  77.         <name><![CDATA[John]]></name>
  78.         <age>21</age>
  79.         <height unit="feet"><![CDATA[6.0]]></height>
  80.       </client>
  81.     </info>
  82.    
  83.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement