Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

xml - Access @attributes data in SimpleXMLElement in PHP

Just wanted to start by saying I have read a LOT of the questions on this site about this exact problem, but I'm still struggling to apply it to my scenario. If someone could help me out, that'd be great! :)

I am trying to extract data from the following XML:

$myXML = '<?xml version="1.0" encoding="UTF-8"?>
<products><product uri="https://192.168.110.201:9630/api/products/1807/" id="1807"    resource_type="current"><code>DEMO - MC700X/A</code><flags><inventoried>true</inventoried><editable_sell>false</editable_sell><master_model>false</master_model></flags><sell_price>0.00</sell_price><description>Apple MC700X/A Demo</description><inventory><available>7</available><reserved>0</reserved><coming_for_stock>2.0</coming_for_stock><coming_for_customer>0.0</coming_for_customer><warehouses>0</warehouses><total>7</total></inventory><product_photos/></product></products>';

I am using SimpleXML to put it into a PHP variable (object?) like this:

$xml = new SimpleXMLElement($myXML);

If I do a:

echo '<pre>';
print_r($xml);
echo '</pre>';

I get the following returned:

SimpleXMLElement Object
(
    [product] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [uri] => https://192.168.110.201:9630/api/products/1807/
                    [id] => 1807
                    [resource_type] => current
                )

            [code] => DEMO - MC700X/A
            [flags] => SimpleXMLElement Object
                (
                    [inventoried] => true
                    [editable_sell] => false
                    [master_model] => false
                )

            [sell_price] => 0.00
            [description] => Apple MC700X/A Demo
            [inventory] => SimpleXMLElement Object
                (
                    [available] => 7
                    [reserved] => 0
                    [coming_for_stock] => 2.0
                    [coming_for_customer] => 0.0
                    [warehouses] => 0
                    [total] => 7
                )

            [product_photos] => SimpleXMLElement Object
                (
                )

        )

)

Now, when I try to access that data programatically, the following works fine:

// This returns the value as expected
echo '<pre>';
echo($xml->product->code);
echo '<br>';
echo($xml->product->sell_price);
echo '<br>';
echo($xml->product->inventory->available);
echo '<br>';
echo '</pre>';

This returns:

DEMO - MC700X/A
0.00
7

But I need to be able to access the "id" tag in the base "product" element (i.e. the @attributes bit), but can't work it out. I've been reading a lot, and figured that I should be able to use the attributes() method, but I can't work it out exactly.

Trying this didn't work:

echo '<pre>';
echo($xml->attributes());
echo '<br>';
echo '</pre>';

It just returns nothing. Can someone please help me? I want to be able to display the "id" tag.. i.e. what I would expect to be:

echo $xml['product']['@attributes']['id'];

obviously doesn't work either.

Thanks!! John

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you try:

echo (string)$xml->product->attributes()->id;

That should give you access to the attributes.

If you have more than 1 product, it may be

echo (string)$xml->product[0]->attributes()->id;

You can also access the attributes using regular array notation such as:

$xml->product['id']; // instead of $xml->product->attributes()->id

See Example #5 from the SimpleXML Examples as well as the manual page on SimpleXMLElement::attributes() for more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...