更新时间:2023-12-04 14:19:52
从数据库中解码json时,您将获得类型为'stdClass'的对象,而不是SimpleXMLElement :: xpath返回的原始类型'SimpleXMLElement'的对象功能.
When you decode the json from the database, you get an object of type 'stdClass' instead of the original type 'SimpleXMLElement' returned by the SimpleXMLElement::xpath function.
stdClass对象不了解SimpleXMLElement对象用于允许访问属性的伪数组语法.
The stdClass object does not 'know' about the pseudo array syntax used by SimpleXMLElement objects to allow accessing the attributes.
通常,您将使用serialize()和unserialize()函数而不是json_encode/decode将对象存储在数据库中,但是不幸的是,SimpleXMLElements不适用于这些对象.
Normally you would use the serialize() and unserialize() functions instead of json_encode/decode to store objects in a database, but unfortunately, SimpleXMLElements are not working with those.
作为一种选择,为什么不存储实际的xml并在从数据库中获取它之后将其读回SimpleXML:
As an alternative, why not just store the actual xml and read it back to SimpleXML after fetching it from the database:
// convert SimpleXMLElement back to plain xml string
$xml = $simpleXML->asXML();
// ... code to store $xml in the database
// ... code to retrieve $xml from database
// recreate SimpleXMLELement
$simpleXML = simplexml_load_string($xml);