XML_Serializer

From: Date: Thu, 20 May 2010 13:46:07 +0000
Subject: XML_Serializer
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-30546@lists.php.net to get a copy of this message
Hi there, I'm trying to use the XML_SERIALIZER to build the following XML: <?xml version="1.0" encoding="utf-8" ?> <Styles > <Style name="Title" element="H1"> <Attribute name="class" value="Title" /> </Style > <Style name="Header" element="H2"> <Attribute name="class" value="Header" /> </Style > <Style name="Sub-header" element="H3"> <Attribute name="class" value="Sub-header" /> </Style > <Style name="Sub-sub-header" element="H4"> <Attribute name="class" value="Sub-sub-header" /> </Style > <Style name="Intro" element="p"> <Override element="p" /> <Override element="span" /> <Attribute name="class" value="IntroP" /> </Style > <Style name="Body" element="p"> <Override element="p" /> <Override element="span" /> <Attribute name="class" value="BodyP" /> </Style > <Style name="Quotes" element="span"> <Override element="span" /> <Attribute name="class" value="Quotes" /> </Style > <Style name="Caption" element="span"> <Override element="span" /> <Attribute name="class" value="Caption" /> </Style > </Styles> Here is my PHP code: require_once 'XML/Serializer.php'; $serializer_options = array ( 'addDecl' => TRUE, 'encoding' => 'ISO-8859-1', 'indent' => ' ', 'indentAttributes' => '_auto', 'rootName' => 'Styles', 'defaultTagName' => 'Style', 'scalarAsAttributes' => true ); $Serializer = &new XML_Serializer($serializer_options); //x($stylesArray); foreach ($stylesArray as $key=>$val) { $overrides = array(); if (!empty($val['attribute'])) { foreach ($val['attribute'] as $aKey=>$aVal) { $attributes = new AttributeInformation($aVal['name'],$aVal['value']); } } if (!empty($val['override'])) { foreach ($val['override'] as $oKey=>$oVal) { $overrides[] = new OverrideInformation($oVal['name'],$oVal['value']); } } $dataArray[] = new StyleInformation($val['name'],$val['element'],$attributes ); } $status = $Serializer->serialize($dataArray); if (PEAR::isError($status)) { die($status->getMessage()); } // Display the XML document header('Content-type: text/xml'); echo($Serializer->getSerializedData()); die(); // Classes to store style information class StyleInformation { var $name; var $element; var $attribute; function StyleInformation($name = NULL, $element = NULL,$attribute = NULL) { $this->name = $name; $this->element = $element; $this->attribute = $attribute; } } class AttributeInformation { var $name; var $value; function AttributeInformation($name = NULL, $value = NULL) { $this->name = $name; $this->value = $value; } } class OverrideInformation { var $element; function AttributeInformation($element = NULL) { $this->element = $element; } } You can see that I'm using the "scalarAsAttributes" option which helps. However, the problem seems to be with catering for multiple instances of the same tags; for example, "overrides" . It works for "attributes" because it is an object. However it doesn't work for "overrides" which is an array of objects. Is this a limitation in XML_SERIALIZER, or am I using it incorrectly? Thanks, Noel da Costa

« previous php.pear.general (#30546) next »