php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44973 SimpleXMLElement doesn't read attributes if xml nodes has a value
Submitted: 2008-05-12 15:44 UTC Modified: 2009-04-30 19:19 UTC
Votes:11
Avg. Score:4.8 ± 0.4
Reproduced:11 of 11 (100.0%)
Same Version:5 (45.5%)
Same OS:3 (27.3%)
From: norbert_schuetz at bigfoot dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.6 OS: Linux 2.6.18-5-686
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: norbert_schuetz at bigfoot dot com
New email:
PHP Version: OS:

 

 [2008-05-12 15:44 UTC] norbert_schuetz at bigfoot dot com
Description:
------------
PHP Version: PHP 5.2.0-8+etch11 (CLI) from php5-cli_5.2.0-8+etch10_i386.deb
Linux 2.6.18-5-686 #1 SMP Sun Aug 12 21:57:02 UTC 2007 i686 GNU/Linux
Simplexml support => enabled
Revision => $Revision: 1.151.2.22.2.15 $
Schema support => enabled
libexpat1_1.95.8-3.4_i386.deb

Description of problem:
For the second xml product item from the sample XML there is no order_id-attribute in the resulting object, while both first an last items have it.


Reproduce code:
---------------
<?php
$xmlstr=<<<EOXML
<?xml version='1.0' standalone='yes'?>
<products>
    <product order_id="0001"></product>
    <product order_id="0002">PHP book</product>
    <product order_id="0003">
        <name>PHP book</name>
    </product>
</products>
EOXML;


$xml=new SimpleXMLElement($xmlstr);
print_r($xml);
?>


Expected result:
----------------
@attributes array for second item as well

Actual result:
--------------
SimpleXMLElement Object
(
    [product] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [order_id] => 0001
                        )

                )

            [1] => PHP book
            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [order_id] => 0003
                        )

                    [name] => PHP book
                )

        )

)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-13 13:29 UTC] felipe@php.net
The attribute can be accessed, however toString really doesn't show it.

print_r($xml->product[1]);

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [order_id] => 0002
        )

    [0] => PHP book
)

 [2009-04-30 19:19 UTC] jani@php.net
The attributes are there, you're just misusing simplexml:

Try this instead:

foreach ($xml->product as $foo)
    var_dump($foo);

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 08:01:30 2025 UTC