From: Noel da Costa on
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







From: "Daniel O'Connor" on
At a guess, I'd say it's a shortcoming - it's probably using array key
entries to keep everything easy to work with behind the scenes.

I would recommend a different approach - XMLWriter is probably worth looking
into; or just the DOM extension.

On Thu, May 20, 2010 at 11:16 PM, Noel da Costa <noel(a)arc2.co.uk> wrote:

> 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
>
>
>
>
>
>
>
>