PHP Classes

File: example

Recommend this page to a friend!
  Classes of Ivan Enderlin   Clean XML To Array   example   Download  
File: example
Role: Example script
Content type: text/plain
Description: how to use this class
Class: Clean XML To Array
Parse a XML document into a nested array
Author: By
Last change: Remove simple quote around NULL parameter (oops :)).
Date: 17 years ago
Size: 1,791 bytes
 

Contents

Class file image Download
<?php

header
('Content-type: text/plain');

include(
'lib.xml.php');

$xml = new Xml;


// PARSE


// Parse from a source into a variable (default source type : FILE)
$source = '<?xml version="1.0" encoding="utf-8"?>

<test>
  <this a="b">
    <is c="d">
      <a e="f" g="h"> first test</a>
    </is>
    <is>
      <b hello="world">second test</b>
    </is>
  </this>
</test>'
;
$out = $xml->parse($source, NULL);
print_r($out);

/* will output
Array
(
    [this] => Array
        (
            [is] => Array
                (
                    [0] => Array
                        (
                            [a] => first test
                            [a-ATTR] => Array
                                (
                                    [e] => f
                                    [g] => h
                                )
                        )
                    [1] => Array
                        (
                            [b] => second test
                            [b-ATTR] => Array
                                (
                                    [hello] => world
                                )
                        )
                )
            [is-ATTR] => Array
                (
                    [c] => d
                )
        )
    [this-ATTR] => Array
        (
            [a] => b
        )
)
*/


// Parse from a local file
$out = $xml->parse('myfile.xml', 'FILE');
print_r($out);

// Parse from a Web file
$out = $xml->parse('http://site.tld/myfile.xml', 'CURL');
print_r($out);



// ENCODING

// You could specify an encoding type (default : utf-8)
$out = $xml->parse('myfile.xml', 'FILE', 'ISO-8859-15');
print_r($out);

// have fun ;-)

?>