3

I am trying to capture the XML data in a Node Named "listing-id".

I create the object and look at the nodes within the object and they all work except one which has a "-" in the name. Here is how I am trying to capture the data but its not working. Any suggestions?

$xmlObj = simplexml_load_file("http://somewebsite.com/file.xml");
$vehObject = $xmlObj->listings->listing;

//begin for each loop
$stockNo = $vehObject[$i]->listing-id;  //returns "0" instead of real stock number.
$VIN = $vehObject[$i]->vin; // this VIN returns just fine....
//end for each loop

As you can see above I am trying for multiple data points... the VIN (and other fields) works just fine. The stockNo is not returning and the only difference is the "-" from all the other nodes.

Thanks in advance!

Louie
  • 5,920
  • 5
  • 31
  • 45

1 Answers1

3

Hyphens are invalid characters in PHP variable names and object properties. This does not stop some libraries (like SimpleXML and JSON) from creating them though. The solution is to use a string form, eg

$vehObject[$i]->{'listing-id'}

See the example here - http://php.net/manual/simplexml.examples-basic.php#example-5422

Phil
  • 157,677
  • 23
  • 242
  • 245