note 97265 added to class.domdocument
| From: | kristoffer dot two at gmail dot com | Date: | Sun, 11 Apr 2010 01:30:23 +0000 |
| Subject: | note 97265 added to class.domdocument | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168115@lists.php.net to get a copy of this message | ||
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;
}
}
----
Server IP: 80.67.10.226
Probable Submitter: 85.230.45.200
----
Manual Page -- http://www.php.net/manual/en/class.domdocument.php
Edit -- https://master.php.net/note/edit/97265
Del: integrated -- https://master.php.net/note/delete/97265/integrated
Del: useless -- https://master.php.net/note/delete/97265/useless
Del: bad code -- https://master.php.net/note/delete/97265/bad+code
Del: spam -- https://master.php.net/note/delete/97265/spam
Del: non-english -- https://master.php.net/note/delete/97265/non-english
Del: in docs -- https://master.php.net/note/delete/97265/in+docs
Del: other reasons-- https://master.php.net/note/delete/97265
Reject -- https://master.php.net/note/reject/97265
Search -- https://master.php.net/manage/user-notes.php