Gissipi_453 Gissipi_453 - 3 months ago 9
PHP Question

PHP XML parser making keys as capitalized

I have this XML -

<SimpleData>Some data</SimpleData>


I'm converting the above XML to array using -

$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $array);
xml_parser_free($p);
print_r ($array);


I get this output -

Array
(
[0] => Array
(
[tag] => SIMPLEDATA
[type] => complete
[level] => 1
[value] => Some data
)

)


The problem is that [tag] gives the values as SIMPLEDATA. But I don't want it to be capitalized.
It should be the same as it is in the XML.

How to correct it ?

Answer

Disable case folding just after creating the parser:

$p = xml_parser_create();

xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); // <-- disable case folding

xml_parse_into_struct($p, $xml, $array);
xml_parser_free($p);

print_r ($array);

Output:

Array
(
    [0] => Array
        (
            [tag] => SimpleData
            [type] => complete
            [level] => 1
            [value] => Some data
        )

)