php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45023 SimpleXML inconsistent behaviour with multiple xml nodes
Submitted: 2008-05-16 17:56 UTC Modified: 2008-05-16 21:43 UTC
From: norbert_schuetz at bigfoot dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.0 OS: Linux 2.6.18-5-686
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: norbert_schuetz at bigfoot dot com
New email:
PHP Version: OS:

 

 [2008-05-16 17:56 UTC] norbert_schuetz at bigfoot dot com
Description:
------------
PHP 5.2.0-8+etch11 (cli) (built: May 10 2008 10:46:24) 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
libxml2_2.6.27.dfsg-2_i386.deb

Problem description:
--------------------
For multiple identically named XML nodes the data types are not consistent, results are possibly erratic or at least misleading:

* SimpleXML returns the first array element instead of returning the array itself when accessing the node - although var_dump() reports the node to be an array.

* gettype() reports "object" while var_dump() reports "array".

* processing the node with foreach reports the key to be the node name, not the numeric array id reported by print_r().

Also, for single XML nodes SimpleXML

* gettype() reports "object" while var_dump() reports "array".

* The node content can be accessed as $xml->node as well as $xml->node[0]. If $xml->node is the node's content (a string, that is) $xml->node[0] should return the first character of the string. At least the documentation should note the ambitious behavior.


Reproduce code:
---------------
<?php
$xmlstr=<<<EOXML
<?xml version='1.0' standalone='yes'?>
<export>
    <testdata>
        <single>test</single>
        <multiple>one</multiple>
        <multiple>two</multiple>
        <multiple>three</multiple>
    </testdata>
</export>
EOXML;

$xml=new SimpleXMLElement($xmlstr);
print '1. var_dump($xml->testdata):' ."\n";
var_dump($xml->testdata);
print "\n";

print '2. get_type($xml->testdata->single): ';
print gettype($xml->testdata->single) ."\n";
print "\n";

print "3. print \$xml->testdata->single without and with [0]: \n";
print $xml->testdata->single ."\n";
print $xml->testdata->single[0] ."\n";
print "\n";

print '4. var_dump($xml->testdata->single): ';
var_dump($xml->testdata->single);
print "\n";

print '5. get_type($xml->testdata->multiple): ';
print gettype($xml->testdata->multiple) ."\n";
print "\n";

print '6. var_dump($xml->testdata->multiple): ';
var_dump($xml->testdata->multiple);
print "\n";

print '7. foreach($xml->testdata->multiple ...):' ."\n";
foreach ($xml->testdata->multiple as $key=>$item) {
    print $key .' = ' . $item ."\n";
}




Expected result:
----------------
6. var_dump($xml->testdata->multiple): object(SimpleXMLElement)#4 (1) 
array(3) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "two"
  [2]=>
  string(5) "three"
}

7. foreach($xml->products->multiple ...):
0 = one
1 = two
2 = three


Actual result:
--------------
1. var_dump($xml->testdata):
object(SimpleXMLElement)#2 (2) {
  ["single"]=>
  string(4) "test"
  ["multiple"]=>
  array(3) {
    [0]=>
    string(3) "one"
    [1]=>
    string(3) "two"
    [2]=>
    string(5) "three"
  }
}

2. get_type($xml->testdata->single): object

3. print $xml->testdata->single without and with [0]:
test
test

4. var_dump($xml->testdata->single): object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(4) "test"
}

5. get_type($xml->testdata->multiple): object

6. var_dump($xml->testdata->multiple): object(SimpleXMLElement)#4 (1) {
  [0]=>
  string(3) "one"
}

7. foreach($xml->testdata->multiple ...):
multiple = one
multiple = two
multiple = three


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-16 21:43 UTC] jani@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.


 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Jun 09 07:03:39 2023 UTC