note 97265 deleted from class.domdocument by degeberg
| From: | degeberg@php.net | Date: | Sun, 11 Apr 2010 10:54:16 +0000 |
| Subject: | note 97265 deleted from class.domdocument by degeberg | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168128@lists.php.net to get a copy of this message | ||
Note Submitter: kristoffer dot two at gmail dot com
----
A simple class I made for a project to parse a DOM document into a array.
<?php
/**
*
* @author krol
* $parser = new DOMParser();
* $parser->parseFile( '/path/to/file.xml' );
* $documentArray = $parser->getArray();
*
*/
class DOMParser
{
private $xml;
private $currentFile;
public function __construct()
{
$this->__init(); // Call this from clear aswell.
}
public function parseFile($file)
{
$this->currentFile = $file; // Keep the file for this current session.
$this->xml->loadXML(file_get_contents($file)); // Create a DOM Object.
}
public function getArray()
{
$domArray = array();
$this->DOM2array($this->xml, $domArray); // Parse into struct
if (!is_array($domArray) && count($domArray) == 0) {
throw new Exception("Could not translate file: ". $this->currentFile . "Into
struct");
}
return $domArray;
}
public function newDOM()
{
$this->__init();
return $this->xml;
}
private function __init()
{
unset($this->currentFile);
$this->xml = new DOMDocument();
}
public function clear()
{
$this->__init(); // Re-initialize this object
}
public function createMarkup($element, $inner = "", $attributes = array(), $handler =
"text")
{
$output = "";
$attribute = "";
if(count($attributes) > 0){
foreach($attributes as $attri => $value) {
$attribute .= $attri.'="'.$value.'"';
}
}
switch ($handler) {
case 'element' :
case 'text' :
$output = "<$element $attribute>".$inner."</$element>";
break;
case 'open' :
$output = "<$element $attribute>";
break;
case 'close' :
$output = "</$element>";
break;
}
return $output;
}
private function DOM2array(DOMNode $node, &$domArray)
{
// Fail checking.
if (!is_array($domArray)){
$domArray = array(); // Is now array..
}
switch ($node->nodeType) {
case XML_DOCUMENT_NODE :
$this->xmlDocumentNode($node, $domArray);
break;
case XML_ELEMENT_NODE :
$this->xmlElementNode($node, $domArray);
break;
case XML_ATTRIBUTE_NODE :
$this->xmlAttrNode($node, $domArray);
break;
case XML_TEXT_NODE :
$this->xmlTextNode($node, $domArray);
break;
}
}
private function xmlDocumentNode(DOMDocument $node, &$domArray)
{
// Doing some readability.
$domArray['document'] = array();
$children = $node->childNodes;
for($i = 0; $i < $children->length; $i++) {
$child = $children->item($i);
// Sends the $child and $domArray back to DOM2Array this time $domArray
// Is sent back with another position. So all operations
// Are done on the correct depth of the array.
$this->DOM2Array($child, $domArray['document']);
}
}
private function xmlElementNode(DOMNode $node, &$domArray)
{
$domArray['#name'] = $node->nodeName;
$domArray['#prefix'] = $node->prefix;
$domArray['children'] = array();
$children = $node->childNodes;
for($i = 0; $i < $children->length; $i++) {
$child = $children->item($i);
if($child->nodeType != XML_TEXT_NODE) {
$this->DOM2Array($child, $domArray['children'][$i]);
}
}
if ($node->hasAttributes()) {
$attributes = $node->attributes;
$i = 0;
foreach ($attributes as $name => $attr) {
$this->DOM2Array($attr, $domArray['attributes'][$i++]);
}
}
if ($children->length == 1) {
$child = $children->item(0);
$this->DOM2Array($child, $domArray);
}
}
private function xmlAttrNode(DOMAttr $node, &$domArray)
{
$domArray['#name'] = $node->name;
$domArray['#value'] = $node->value;
$domArray['#prefix'] = $node->prefix;
}
private function xmlTextNode(DOMText $node, &$domArray)
{
$domArray['#value'] = $node->nodeValue;
$domArray['#prefix'] = $node->prefix;
}
}