I understand how to call children in .xml documents by parsing with simplexml, however I'm having a problem calling a child with a name that looks like this:name2.
To be more specific, I'm trying to echo nhc:center from this .xml document: http://www.nhc.noaa.gov/nhc_ep4.xml.
Currently my code looks like this:
<?php
$xml=simplexml_load_file("http://www.nhc.noaa.gov/nhc_ep4.xml") or die("Error: Cannot create object");
echo $xml->channel->title[0] . "<br>";
echo $xml->channel->description[0] . "<br><br>";
echo $xml->channel->item[0]->nhc:Cyclone->nhc:center . "<br>";
?>
The first three lines work perfectly, however I return this error when trying to call nhc:center:
Parse error: syntax error, unexpected ':', expecting ',' or ';' in C:\xampp\htdocs\test.php on line 5
I'm sure it's something simple I'm missing here, any help is greatly appreciated!
Edit 2
A day after this answer was accepted, a less "hacky" answer was posted here: https://stackoverflow.com/a/44813107/713874
Original Reply
Try wrapping the name in curly-brackets and quotation marks, like this:
echo $xml->channel->item[0]->{'nhc:Cyclone'}->{'nhc:center'} . "<br>";
Source: How do I access this object property with a hyphenated name?
Edit: I noticed your domains were all public and saw it still wasn't grabbing your info. Weird. I did find this work-around that solved it:
$xml = simplexml_load_string(str_replace("nhc:", "nhc", file_get_contents("http://www.nhc.noaa.gov/nhc_ep4.xml")));