Php simplexml with dot character in element in xml

In the lower XML format, how can we access the News.Env element from XMLReader in php?

$xmlobj->News->News.Env gives Env, which is wrong.

<?xml version="1.0" encoding="utf-8"?>
<News>
  <News.Env>abc</News.Env>
</News>
+5
source share
2 answers

This is because the dot .is a string concatenator in php. In your case, it tries to concatenate $xmlobj->News->News(which does not exist and is therefore empty) with a constant Env(which also does not exist and is treated as a string). You will receive a notification about this using the appropriate error_level)

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

or shorter

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

Update: if you use SimpleXML(and according to the syntax that you do this), it $xmlobj"starts" with the element News- (root-).

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

-

$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'});
0