fluidxml icon indicating copy to clipboard operation
fluidxml copied to clipboard

Add ability to export XML to array or object (stdClass)

Open kinekt4 opened this issue 4 years ago • 2 comments

This PR is in relation to a suggestion raised in issue #28.

It allows you to export the XML in either an array or object. It introduces two new public functions:

  • toArray()
  • toObject()

It is dependant on the PEAR Library pear/xml_serializer.

Given the following XML:

<animal operation="create">
    <type>Herbivore</type>
    <attribute xmlns:foo="http://namespace.foo">
      <foo:legs>4</foo:legs>
      <foo:head>1</foo:head>
    </attribute>
</animal>

Example usage:

$fluid = new FluidXml();
$fluid->addChild($xml);
$arr = $fluid->toArray();
print_r($arr);

Response

Array
(
    [animal] => Array
        (
            [@xmlns:foo] => http://namespace.foo
            [@operation] => create
            [type] => Herbivore
            [attribute] => Array
                (
                    [@xmlns:foo] => http://namespace.foo
                    [foo:legs] => 4
                    [foo:head] => 1
                )

        )

)

kinekt4 avatar Sep 04 '19 09:09 kinekt4

Coverage Status

Coverage decreased (-2.5%) to 97.317% when pulling 87a90a84481c647239739a5845b787ae30bfbd6a on denormalizer:ENH-xml-export into 22fe7678d7c9fa6c17cd53d04327d1dc1a44ebf7 on servo-php:master.

coveralls avatar Sep 04 '19 09:09 coveralls

Instead of the two proposed functions, I've consolidated the two proposed functions into one function: exportTo().

Mainly to reduce ambiguity between array() and toArray().

The usage has changed slightly.

Given the following XML:

<animal operation="create">
    <type>Herbivore</type>
    <attribute xmlns:foo="http://namespace.foo">
      <foo:legs>4</foo:legs>
      <foo:head>1</foo:head>
    </attribute>
</animal>

Example usage:

$fluid = new FluidXml();
$fluid->addChild($xml);
$arr = $fluid->exportTo('array');
print_r($arr);

Response

Array
(
    [animal] => Array
        (
            [@xmlns:foo] => http://namespace.foo
            [@operation] => create
            [type] => Herbivore
            [attribute] => Array
                (
                    [@xmlns:foo] => http://namespace.foo
                    [foo:legs] => 4
                    [foo:head] => 1
                )

        )

)

kinekt4 avatar Sep 11 '19 01:09 kinekt4