Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple xml - object or not an object - when is this the case?

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
Working on a script to fetch info from a service provider - All is well as far as getting to the provider, sending what needs to be sent ...

My confusion rises when I try to pare the returned XML object:
Code:
object(SimpleXMLElement)#2 (22) {
  ["@attributes"]=>
  array(1) {
    ["Version"]=>
    string(6) "160126"
  }
  ["errMsg"]=>
  object(SimpleXMLElement)#5 (0) {
  }
  ["RateQuoteNumber"]=>
  string(12) "201610036706"
  ["EffectiveDate"]=>
  string(10) "10/03/2016"
  ["shippingDays"]=>
  string(1) "2"
  ["destinationServiceCenter"]=>
  string(9) "ALBANY NY"
  ["originatingServiceCenter"]=>
  string(15) "WEST CHESTER PA"
  ["totalCharge"]=>
  string(7) "1734.54"
  ["ClassRates"]=>
  object(SimpleXMLElement)#6 (1) {
    ["Class"]=>
    string(5) "57.34"
  }
  ["RateAsWeight"]=>
  string(4) "2500"
  ["DeficitCharges"]=>
  object(SimpleXMLElement)#7 (0) {
  }
  ["FreightCharge"]=>
  string(6) "1433.5"
  ["FuelSurcharge"]=>
  string(6) "301.04"
  ["FerryCharge"]=>
  object(SimpleXMLElement)#8 (0) {
  }
 ...
 ...
 ...
The complete XML object is captured in PHP variable $xml - the above show the top portion of the object obtained by using var_dump().

Now, if I use:
Code:
$err=$xml->Msg;
$qNo=$xml->RateQuoteNumber;
I can echo both variables and get expected content.

If I do
Code:
$total=$xml->totalCharge;
and echo $total, it tells me it is an object.

I have been looking at this for a while but cannot see how totalCharge is defined as an object.

I then use var_dump() to show $total and I get
object(SimpleXMLElement)#3 (1) { [0]=> string(7) "1734.54" }

Worst thing is that $total[0] nor $total->0 can be referenced.

What am I missing here?


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
BTW, I got around this by using
Code:
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}

and then calling this method as $amt=xml_attribute($total,0);

Figure I may as well offer the solution I came up with ... Still confused as to why totalCharge is looked at as an object ...

--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top