简体   繁体   中英

PHP, json_encode, json_decode of SimpleXML Object

A function in my application does the following:

  • Capture Web Page using Snoopy
  • Load result into DOMDocument
  • Load DOMDocument into Simple XML Object
  • Run XPath to isolate section of document required
  • json_encode the result and save to database for later use.

My problem arises when recovering this block from the database, and decoding it. I can see the @attributes when I var_dump the object, but cannot find a combination of commands that allows me to access them.

Error message is: Fatal error: Cannot use object of type stdClass as array

Below is a sample of my object. I have tried, amongst other what used to work.

echo $obj['class'];

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [class] => race_idx_hdr
        )

    [img] => stdClass Object
        (
            [@attributes] => stdClass Object
                (
                    [src] => /Images/Icons/i_blue_bullet.gif
                    [alt] => image
                    [title] => United Kingdom
                )

        )

    [a] => Fast Cards
)

I actually don't really understand what you're trying to do and where the error is thrown, but to access the properties of your object you can use

echo $obj->{'@attributes'}->class; // prints "race_idx_hdr"
echo $obj->img->{'@attributes'}->src; // prints "/Images/Icons/i_blue_bullet.gif"
echo $obj->img->{'@attributes'}->alt; // prints "image"
echo $obj->img->{'@attributes'}->title; // prints "United Kingdom"
echo $obj->a; // prints "Fast Cards"

This weird syntax ( $obj->{'@attributes'} ) is required because the @ -symbol is reserved in PHP and cannot be used for identifiers.

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.

The stdClass object does not 'know' about the pseudo array syntax used by SimpleXMLElement objects to allow accessing the attributes.

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.

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

If an object is converted to an array, the result is an array whose elements are the object's properties.

$asArray = (array)$myObj;
echo $asArray['@attribute'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM