sabre-io / xml

sabre/xml is an XML library that you may not hate.
http://sabre.io/xml/
BSD 3-Clause "New" or "Revised" License
516 stars 77 forks source link

How can parse double elements naming? #130

Closed diseltoofast closed 7 years ago

diseltoofast commented 7 years ago

Hi! I'm parse the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<export xmlns="http://test.com/types/1">
    <notify>
        <listInfo>
            <list>
                <date>2017-07-01T10:24:04+06:00</date>
                <place>Test</place>
            </list>
        </listInfo>
        <law>
            <requirements>
                <requirement>
                    <one>
                        <amount>10</amount>
                        <listInfo>Lorem 1</listInfo>
                    </one>
                    <two>
                        <amount>20</amount>
                        <listInfo>Lorem 2</listInfo>
                    </two>
                </requirement>
            </requirements>
        </law>
    </notify>
</export>

<listInfo> is double name element and has different types. How can parse for this xml, any ideas?

My code:

$service = new \Sabre\Xml\Service();
$service->elementMap = [
    '{http://test.com/types/1}export' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}notify' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}listInfo' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}list' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}law' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}requirements' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\repeatingElements($reader, '{http://test.com/types/1}requirement');
    },
    '{http://test.com/types/1}requirement' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}one' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
    '{http://test.com/types/1}two' => function(\Sabre\Xml\Reader $reader) {
        return \Sabre\Xml\Deserializer\keyValue($reader, 'http://test.com/types/1');
    },
];
$result = $service->parse($xml);

Output:

Array
(
    [notify] => Array
        (
            [listInfo] => Array
                (
                    [list] => Array
                        (
                            [date] => 2017-07-01T10:24:04+06:00
                            [place] => Test
                        )
                )
            [law] => Array
                (
                    [requirements] => Array
                        (
                            [0] => Array
                                (
                                    [one] => Array
                                        (
                                            [amount] => 10
                                            [listInfo] => Array
                                                (
                                                )
                                        )
                                    [two] => Array
                                        (
                                            [amount] => 20
                                            [listInfo] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
)

<listInfo> value is empty.

diseltoofast commented 7 years ago

I found solution https://github.com/fruux/sabre-xml/issues/127 thanks