RomaricDrigon\MetaYaml\Loader\XmlLoader::load PHP Method

load() public method

public load ( $string, $ignore_first_node = false )
    public function load($string, $ignore_first_node = false)
    {
        // we will parse XML using the convention (cf doc.)
        // elements and attributes are stored in arrays
        // multiple elements will give a prototype, using "_key" as name -> else nodes will be merged
        // namespace are ditched
        // empty nodes are suppressed
        // node _value
        if (!$ignore_first_node) {
            $string = '<mock_tag>' . $string . '</mock_tag>';
        }
        $sxi = simplexml_load_string($string, 'SimpleXmlIterator');
        if ($sxi === false) {
            throw new \Exception('Error in XmlLoader : XML seems to be invalid');
        }
        return $this->xmlToArray($sxi);
    }

Usage Example

Esempio n. 1
0
    public function testAttributes()
    {
        $this->if($object = new testedClass())->and($xml = <<<EOT
<fleurs>
    <roses couleur="rose">
        <opera>une rose</opera>
        <sauvage>
            <des_bois>une autre rose</des_bois>
        </sauvage>
    </roses>
    <violette couleur="violette">une violette</violette>
</fleurs>
EOT
)->then->object($object)->isInstanceOf('RomaricDrigon\\MetaYaml\\Loader\\XmlLoader')->array($object->load($xml))->isEqualTo(array('fleurs' => array('roses' => array('couleur' => 'rose', 'opera' => 'une rose', 'sauvage' => array('des_bois' => 'une autre rose')), 'violette' => array('couleur' => 'violette', '_value' => 'une violette'))));
    }