Mithrand1r
Mithrand1r

Reputation: 2353

Convert SimpleXmlElement with one attribute to string

I have some XML to be parsed using SimpleXmlElement . Since now everything works as it should. I get XML and all fields.

Now I need to load values I have parsed into database, I am using PDO and this is causing some problems because I am trying to save object of SimpleXmlElement into field which should be text.

Here is my code sample:

$score = $cvss->base_metrics->score;

echo $score result in:

9.3

although var_dump($score) gives

object(SimpleXMLElement)#9(1) {[0]=> string(3) "9.3"}

using serialize() on this function result in :

Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed

each try of accesing [0] field directly fails... I need 9.3 to be string in order to be able to create PDO object. Can anyone give me a hint?

Upvotes: 0

Views: 74

Answers (1)

Tomanow
Tomanow

Reputation: 7357

You can use __toString()

public string SimpleXMLElement::__toString ( void )

Returns text content that is directly in this element. Does not return text content that is inside this element's children.

Upvotes: 1

Related Questions