×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

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

(OP)
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
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close