[ create a new paste ] login | about

Link: http://codepad.org/PpyewkVd    [ raw code | output | fork ]

PHP, pasted on Nov 14:
<?php
class xmlizer extends DomDocument {

	function __construct() {
		parent::__construct();
	}

	function node_create($arr, $items = null) {
		if (is_null($items))
			$items = $this->appendChild($this->createElement("items"));

		foreach($arr as $element => $value) {
			$element = is_numeric( $element ) ? "node" : $element;
			$fragment = $this->createElement($element,  (is_array($value) ? null : $value) );
                        $items->appendChild($fragment);

			if (is_array($value)) {
				self::node_create($value, $fragment);
			}
		}
	}

	public function __toString() {
		return html_entity_decode($this->saveXML());
	}

}


for($i=0;$i<5;$i++) {
	$j = $i+1;
	$array['example'][] = array(
		"id" => $j,
		"title" => "Title $j",
		"description" => "<p>Text <strong>string</strong> nr. $j with <em>some</em> <code>HTML code</code>.</p>",
		);
}

header("Content-Type:text/xml");
$xml = new xmlizer();
$xml->node_create($array);
echo $xml;


Output:
1
2
<?xml version="1.0"?>
<items><example><node><id>1</id><title>Title 1</title><description><p>Text <strong>string</strong> nr. 1 with <em>some</em> <code>HTML code</code>.</p></description></node><node><id>2</id><title>Title 2</title><description><p>Text <strong>string</strong> nr. 2 with <em>some</em> <code>HTML code</code>.</p></description></node><node><id>3</id><title>Title 3</title><description><p>Text <strong>string</strong> nr. 3 with <em>some</em> <code>HTML code</code>.</p></description></node><node><id>4</id><title>Title 4</title><description><p>Text <strong>string</strong> nr. 4 with <em>some</em> <code>HTML code</code>.</p></description></node><node><id>5</id><title>Title 5</title><description><p>Text <strong>string</strong> nr. 5 with <em>some</em> <code>HTML code</code>.</p></description></node></example></items>


Create a new paste based on this one


Comments: