Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php simplexml with dot character in element in xml

Tags:

php

simplexml

With the below xml format how we can access News.Env element from XMLReader in php?

$xmlobj->News->News.Env gives Env which is not correct.

<?xml version="1.0" encoding="utf-8"?>
<News>
  <News.Env>abc</News.Env>
</News>
like image 637
jit Avatar asked Jun 30 '11 07:06

jit


2 Answers

This is because the dot . is the string concatenator in php. In your case it tries to concatenate $xmlobj->News->News (which doesn't exists and is therefore empty) with the constant Env (which doesn't exists too and is treated as a string. you would get a notice about this with an appropriate error_level)

$tmp = 'News.Env';
$xmlobj->News->$tmp;

or in short

$xmlobj->News->{'News.Env'};

Update: If you use SimpleXML (and according the syntax you do it) it $xmlobj "starts" with the News-(root-)Element.

$xmlobj->{'News.Env'};
like image 121
KingCrunch Avatar answered Sep 20 '22 18:09

KingCrunch


Try something like

$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<News>
    <News.Env>abc</News.Env>
</News>
XML;

$xml = simplexml_load_string($string);

print_r($xml->{'News.Env'});
like image 36
Eric Herlitz Avatar answered Sep 20 '22 18:09

Eric Herlitz