cvs: pear /XML_Serializer Serializer.php /XML_Serializer/examples serializeWithNamespace.php

From: Date: Thu, 23 Dec 2004 12:45:53 +0000
Subject: cvs: pear /XML_Serializer Serializer.php /XML_Serializer/examples serializeWithNamespace.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-27783@lists.php.net to get a copy of this message
schst Thu Dec 23 07:45:53 2004 EDT Added files: /pear/XML_Serializer/examples serializeWithNamespace.php Modified files: /pear/XML_Serializer Serializer.php Log: added new namespace option http://cvs.php.net/diff.php/pear/XML_Serializer/Serializer.php?r1=1.30&r2=1.31&ty=u Index: pear/XML_Serializer/Serializer.php diff -u pear/XML_Serializer/Serializer.php:1.30 pear/XML_Serializer/Serializer.php:1.31 --- pear/XML_Serializer/Serializer.php:1.30 Thu Dec 23 05:28:03 2004 +++ pear/XML_Serializer/Serializer.php Thu Dec 23 07:45:52 2004 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php-tools.net> | // +----------------------------------------------------------------------+ // -// $Id: Serializer.php,v 1.30 2004/12/23 10:28:03 schst Exp $ +// $Id: Serializer.php,v 1.31 2004/12/23 12:45:52 schst Exp $ /** * uses PEAR error management @@ -129,7 +129,8 @@ 'attributesArray' => null, // all values in this key will be treated as attributes 'contentName' => null, // this value will be used directly as content, instead of creating a new tag, may only be used in conjuction with attributesArray 'tagMap' => array(), // tag names that will be changed - 'encodeFunction' => null + 'encodeFunction' => null, // function that will be applied before serializing + 'namespace' => null, // namespace to use ); /** @@ -247,6 +248,11 @@ // start depth is zero $this->_tagDepth = 0; + $rootAttributes = $this->options['rootAttributes']; + if (is_array($this->options['namespace'])) { + $rootAttributes['xmlns:'.$this->options['namespace'][0]] = $this->options['namespace'][1]; + } + $this->_serializedData = ''; // serialize an array if (is_array($data)) { @@ -256,7 +262,7 @@ $tagName = 'array'; } - $this->_serializedData .= $this->_serializeArray($data, $tagName, $this->options['rootAttributes']); + $this->_serializedData .= $this->_serializeArray($data, $tagName, $rootAttributes); } // serialize an object elseif (is_object($data)) { @@ -265,7 +271,7 @@ } else { $tagName = get_class($data); } - $this->_serializedData .= $this->_serializeObject($data, $tagName, $this->options['rootAttributes']); + $this->_serializedData .= $this->_serializeObject($data, $tagName, $rootAttributes); } // add doctype declaration @@ -278,12 +284,7 @@ // build xml declaration if ($this->options['addDecl']) { $atts = array(); - if (isset($this->options['encoding']) ) { - $encoding = $this->options['encoding']; - } else { - $encoding = null; - } - $this->_serializedData = XML_Util::getXMLDeclaration('1.0', $encoding) + $this->_serializedData = XML_Util::getXMLDeclaration('1.0', $this->options['encoding']) . $this->options['linebreak'] . $this->_serializedData; } @@ -533,6 +534,14 @@ */ function _createXMLTag( $tag, $replaceEntities = true ) { + if ($this->options['namespace'] !== null) { + if (is_array($this->options['namespace'])) { + $tag['qname'] = $this->options['namespace'][0] . ':' . $tag['qname']; + } else { + $tag['qname'] = $this->options['namespace'] . ':' . $tag['qname']; + } + } + if ($this->options['indentAttributes'] !== false) { $multiline = true; $indent = str_repeat($this->options['indent'], $this->_tagDepth); http://cvs.php.net/co.php/pear/XML_Serializer/examples/serializeWithNamespace.php?r=1.1&p=1 Index: pear/XML_Serializer/examples/serializeWithNamespace.php +++ pear/XML_Serializer/examples/serializeWithNamespace.php <?PHP /** * Example that creates tags with a namespace * * @author Stephan Schmidt <schst@php.net> */ error_reporting(E_ALL); require_once '../Serializer.php'; $options = array( 'indent' => ' ', 'linebreak' => "\n", 'defaultTagName' => 'item', 'namespace' => 'foo' ); $foo = new stdClass; $foo->value = 'My value'; $foo->xml = 'cool'; $foo->obj = new stdClass; $foo->arr = array(); $foo->zero = 0; $serializer = &new XML_Serializer($options); $result = $serializer->serialize($foo); if( $result === true ) { $xml = $serializer->getSerializedData(); } echo '<pre>'; print_r( htmlspecialchars($xml) ); echo '</pre>'; // also pass the URI $serializer->setOption('namespace', array('bar', 'http://pear.php.net/package/XML_Serializer')); $result = $serializer->serialize($foo); if( $result === true ) { $xml = $serializer->getSerializedData(); } echo '<pre>'; print_r( htmlspecialchars($xml) ); echo '</pre>'; ?>

« previous php.pear.cvs (#27783) next »