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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Simplet Object - What is up with this? 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have this
Code:
object(SimpleXMLElement)#2 (9) {
  ["ratequoteline"]=>
  array(5) {
    [0]=>
    ...
  }
  ["servicetype"]=>
  string(14) "NORMAL SERVICE"
  ["totalweight"]=>
  string(4) "2500"
  ["totalpallets"]=>
  string(2) "10"
  ["totalpieces"]=>
  string(2) "10"
  ["quotetotal"]=>
  string(6) "433.26"
  ["quotedatetime"]=>
  array(2) {
    [0]=>
    string(29) "12/18/2016 13:01:29.290-05:00"
    [1]=>
    string(29) "12/18/2016 13:01:29.290-05:00"
  }
  ["busdays"]=>
  string(1) "7"
  ["quoteversion"]=>
  string(18) "ratequote-20160926"
}
I am trying to extract quotetotal, quoteversion, busdays and servicetype
Code:
$amt=trim($xml->quotetotal);
$msg=trim($xml->servicetype);
$qn=trim($xml->quoteversion);
$qd=trim($xml->busdays);

Why in the world are they coming out as Objects not as strings?

I keep getting parse errors saying that they are objects - If I try to read it as an xml-simple-object it fails as well.

What am I missing?




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

I suppose quotetotal was a tag in the XML and you want its content. But that being a node, it may also have attributes, so PHP must store it internally as object. To access the node's content you have to use its [tt]__toString()[/tt] method. But that being a magic method, is also triggered automatically when the context indicates its need.

Code:
[gray]// call it explicitly[/gray]
[navy]$amt[/navy][teal]=[/teal][COLOR=orange]trim[/color][teal]([/teal][navy]$xml[/navy][teal]->[/teal]quotetotal[teal]->[/teal][COLOR=orange]__toString[/color][teal]());[/teal]

[gray]// force to be called[/gray]
[navy]$amt[/navy][teal]=[/teal][COLOR=orange]trim[/color][teal](([/teal]string[teal])[/teal] [navy]$xml[/navy][teal]->[/teal]quotetotal[teal]);[/teal]


Feherke.
feherke.ga
 
@feherke,

Exactly the solution I needed.

I have used _toString() before ... cannot believe I did not even think of it.

Thanks!


--
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