cvs: pear /XML_Serializer Unserializer.php /XML_Serializer/doc todo.txt /XML_Serializer/examples example.xml unserializeWithAttributes.php
| From: | Stephan Schmidt | Date: | Tue, 09 Sep 2003 16:12:54 +0000 |
| Subject: | cvs: pear /XML_Serializer Unserializer.php /XML_Serializer/doc todo.txt /XML_Serializer/examples example.xml unserializeWithAttributes.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-12584@lists.php.net to get a copy of this message | ||
schst Tue Sep 9 12:12:54 2003 EDT
Added files:
/pear/XML_Serializer/examples example.xml
unserializeWithAttributes.php
Modified files:
/pear/XML_Serializer Unserializer.php
/pear/XML_Serializer/doc todo.txt
Log:
added feature to unserialize attributes
Index: pear/XML_Serializer/Unserializer.php
diff -u pear/XML_Serializer/Unserializer.php:1.2 pear/XML_Serializer/Unserializer.php:1.3
--- pear/XML_Serializer/Unserializer.php:1.2 Wed Sep 3 14:58:55 2003
+++ pear/XML_Serializer/Unserializer.php Tue Sep 9 12:12:53 2003
@@ -16,7 +16,7 @@
// | Authors: Stephan Schmidt <schst@php-tools.net> |
// +----------------------------------------------------------------------+
//
-// $Id: Unserializer.php,v 1.2 2003/09/03 18:58:55 schst Exp $
+// $Id: Unserializer.php,v 1.3 2003/09/09 16:12:53 schst Exp $
require_once 'PEAR.php';
require_once 'XML/Parser.php';
@@ -101,10 +101,14 @@
* @var array $_defaultOptions
*/
var $_defaultOptions = array(
- "complexType" => "array",
- "keyAttribute" => "_originalKey",
- "typeAttribute" => "_type",
- "classAttribute" => "_class"
+ "complexType" => "array",
+ "keyAttribute" => "_originalKey",
+ "typeAttribute" => "_type",
+ "classAttribute" => "_class",
+ "parseAttributes" => false,
+ "attributesArray" => false,
+ "prependAttributes" => "",
+ "contentName" => "_content"
);
/**
@@ -276,20 +280,37 @@
*/
function startHandler($parser, $element, $attribs)
{
- if (!isset($attribs[$this->options["typeAttribute"]])) {
- $attribs[$this->options["typeAttribute"]] = "string";
+ if (isset($attribs[$this->options["typeAttribute"]])) {
+ $type = $attribs[$this->options["typeAttribute"]];
+ } else {
+ $type = "string";
}
+
$this->_depth++;
$this->_dataStack[$this->_depth] = null;
$val = array(
"name" => $element,
"value" => null,
- "type" =>
$attribs[$this->options["typeAttribute"]],
+ "type" => $type,
"childrenKeys" => array(),
"aggregKeys" => array()
);
+ if ($this->options["parseAttributes"] == true && (count($attribs) >
0)) {
+ $val["children"] = array();
+ $val["type"] = $this->options["complexType"];
+
+ if ($this->options["attributesArray"] != false) {
+ $val["children"][$this->options["attributesArray"]] =
$attribs;
+ } else {
+ foreach ($attribs as $attrib => $value) {
+
$val["children"][$this->options["prependAttributes"].$attrib] = $value;
+ }
+ }
+ }
+
+
if (isset($attribs[$this->options["keyAttribute"]])) {
$val["name"] = $attribs[$this->options["keyAttribute"]];
}
@@ -314,6 +335,7 @@
$value = array_pop($this->_valStack);
$data = trim($this->_dataStack[$this->_depth]);
+
// adjust type of the value
switch(strtolower($value["type"])) {
/*
@@ -328,6 +350,10 @@
$value["value"] = new stdClass;
}
+ if ($data !== '') {
+ $value["children"][$this->options["contentName"]] =
$data;
+ }
+
// set properties
foreach($value["children"] as $prop => $propVal) {
$value["value"]->$prop = $propVal;
@@ -344,6 +370,10 @@
* unserialize an array
*/
case "array":
+ if ($data !== '') {
+ $value["children"][$this->options["contentName"]] =
$data;
+ }
+
$value["value"] = $value["children"];
break;
@@ -420,6 +450,5 @@
{
$this->_dataStack[$this->_depth] .= $cdata;
}
-
}
?>
Index: pear/XML_Serializer/doc/todo.txt
diff -u pear/XML_Serializer/doc/todo.txt:1.2 pear/XML_Serializer/doc/todo.txt:1.3
--- pear/XML_Serializer/doc/todo.txt:1.2 Wed Sep 3 14:58:56 2003
+++ pear/XML_Serializer/doc/todo.txt Tue Sep 9 12:12:54 2003
@@ -2,6 +2,16 @@
--------------------
- find a way to detect references in the structure and add id/idref attributes
- serialize some other structures, especially XPath return values
+- serializes tags with attributes
+ array(
+ "_attributes" => array( "foo" => "bar" ),
+ "child" => "content"
+ );
+ serializes to:
+ <array foo="bar">
+ <foo>bar</foo>
+ </array>
+
- custom serialization of objects
- namespace support
- alternative way to define aggregates
@@ -14,7 +24,7 @@
TODO XML_Unserializer:
----------------------
-- some way to handle attributes (convert them into arrays)
+- add support to convert arrays into objects
- support for xml:space attribute
- set types for certain tags
- namespace support
Index: pear/XML_Serializer/examples/example.xml
+++ pear/XML_Serializer/examples/example.xml
<xml>
<test foo="bar">
<tag>test</tag>
</test>
</xml>
Index: pear/XML_Serializer/examples/unserializeWithAttributes.php
+++ pear/XML_Serializer/examples/unserializeWithAttributes.php
<?PHP
error_reporting(E_ALL);
require_once '../Unserializer.php';
$options = array(
"parseAttributes" => true,
"attributesArray" => "_attributes"
// "prependAttributes" => "att_"
);
// be careful to always use the ampersand in front of the new operator
$unserializer = &new XML_Unserializer($options);
// userialize the document
$status = $unserializer->unserialize("example.xml", true);
echo "<pre>";
print_r($status);
echo "</pre>";
if (PEAR::isError($status)) {
echo "Error: " . $status->getMessage();
} else {
$data = $unserializer->getUnserializedData();
echo "<pre>";
print_r( $data );
echo "</pre>";
}
?>