Convert XML to Array using DOM extension in PHP5

The xml2array class convert the XML stored in a string into an ARRAY. We are using the DOM extension of PHP5. The DOM extension replaced the DOM XML extension available in PHP4. The DOM XML extension is depreciated in PHP5.

<?php
class xml2array {
 
	function xml2array($xml) {
		if (is_string($xml)) {
			$this->dom = new DOMDocument;
			$this->dom->loadXml($xml);
		}
 
		return FALSE;
	}
 
	function _process($node) { 
		$occurance = array();
 
		foreach($node->childNodes as $child) {
			$occurance[$child->nodeName]++;
		}
 
		if($node->nodeType == XML_TEXT_NODE) { 
			$result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'), 
                                     ENT_COMPAT,'ISO-8859-15');
		} 
		else {
			if($node->hasChildNodes()){
				$children = $node->childNodes;
 
				for($i=0; $i<$children->length; $i++) {
					$child = $children->item($i);
 
					if($child->nodeName != '#text') {
						if($occurance[$child->nodeName] > 1) {
							$result[$child->nodeName][] = $this->_process($child);
						}
						else {
							$result[$child->nodeName] = $this->_process($child);
						}
					}
					else if ($child->nodeName == '#text') {
						$text = $this->_process($child);
 
						if (trim($text) != '') {
							$result[$child->nodeName] = $this->_process($child);
						}
					}
				}
			} 
 
			if($node->hasAttributes()) { 
				$attributes = $node->attributes;
 
				if(!is_null($attributes)) {
					foreach ($attributes as $key => $attr) {
						$result["@".$attr->name] = $attr->value;
					}
				}
			}
		}
 
		return $result;
	}
 
	function getResult() {
		return $this->_process($this->dom);
	}
}
?>

We can use this class like this.

<?php
	$obj = new xml2array($XML_string);
	$XML_array = $obj->getResult();
?>

, ,


  1. #1 by Dan Clarke - January 10th, 2011 at 18:04

    Brilliant script, works really really well with a current project we’re working on.
    However, it seems, after using this script, that it removes new lines (\n) from the values of the XML, is there anyway to get these back, or convert them to s?

    Thanks!
    -Dan.

  2. #2 by gkirok - August 8th, 2011 at 19:01

    Hi
    Nice class but has too many notices.
    Please enable notices when you develope

    so i added initializing result and isset check:
    $result = ”;

    if(isset($node->childNodes)) {
    foreach($node->childNodes as $child) {
    $occurance[$child->nodeName] = isset($occurance[$child->nodeName]) ? $occurance[$child->nodeName] + 1 : 1;
    }
    }

  3. #3 by Laurynas - September 28th, 2011 at 11:47

    Very usefull class. Thank you!

    I also modified it like gkirok suggested to remove those warnings

  4. #4 by Steve - December 22nd, 2011 at 22:03

    Handy implementation. Somerthing I’ve noticed is that you might risk to lose data if you receive nodes of XML_CDATA_SECTION_NODE type. I’m afraid such data might be silently dropped. Handling such nodes the same way as XML_TEXT_NODE nodes could be an idea to avoid this. My 2cc.

(will not be published)

 


Submit Comment
Subscribe to comments feed
  1. No trackbacks yet.