Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kinekt4
Copy link

@kinekt4 kinekt4 commented Sep 4, 2019

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
                )

        )

)

@coveralls
Copy link

Coverage Status

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

@kinekt4
Copy link
Author

kinekt4 commented Sep 11, 2019

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
                )

        )

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants