2014-02-27 63 views
0

我有一些XML要使用SimpleXmlElement进行解析。从现在开始,所有事情都应该如此。我得到XML和所有领域。将具有一个属性的SimpleXmlElement转换为字符串

现在我需要我已经解析值加载到database,我使用PDO这也是造成一些问题,因为我想拯救的SimpleXmlElement对象到现场应该是text

这里是我的代码示例:

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

echo $score结果:

9.3 

虽然var_dump($score)在此函数结果使用serialize()

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

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

accesing [0]场的每个尝试直接失败...我需要9.3是字符串,以便能够创建PDO对象。任何人都可以给我一个提示吗?

+0

将结果强制转换为字符串:'$ score =(string)$ cvss-> base_metrics-> score;' – michi

回答

1

您可以使用__toString()

public string SimpleXMLElement::__toString (void) 

返回文本的内容是直接在此元素。不返回此元素的子元素内的文本内容。

+0

__toString()是一种魔术方法,用于定义将对象转换为字符串时的行为。像'$ string =(string)$ simpleXmlElement;'。 – ThW

相关问题