views:

27

answers:

1

Does anyone know how to parse an xml string in php using SimpleXMLElement when the key has a space in it ?

For example,

$xmlString = "<test><this is>a</this is></test>";
$xml = new SimpleXMLElement($xmlString);
print_r($xml);

in the above example, 'this is' causes the parser to go bananas. I'm guessing its because it thinks its a property as is expecting like ??

FOR A BONUS PT, (also if the key is a number.. like '1', the same thing happens)..

+3  A: 

That's because having numbers as elements and elements with spaces is not part of valid XML. You're probably better off running a replacement function on your XML string, converting <(\d+)> to <el_$1>, and replacing spaces in nodes with underscores as well.

mway