php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51837 XMLReader cannot store values in arrays
Submitted: 2010-05-17 10:14 UTC Modified: 2013-02-18 00:34 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: cvb724 at yahoo dot ca Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 5.3.2 OS: CentOS 5
Private report: No CVE-ID: None
 [2010-05-17 10:14 UTC] cvb724 at yahoo dot ca
Description:
------------
I have tried every way possible to store the values from an XML sheet using XMLReader in an array. The XML sheet is valid. I have tried this on two systems now, both with the same problem. Colleagues cannot find anything wrong either. Example code is below:

CODE:

$reader = new XMLReader();
$url = 'http://xml.com/info.php'; // not a real URL
	
$reader->open($url);
	
$i = 0;
$j = 0;
	
while ($reader->read())
{
	if ($reader->nodeType == XMLReader::ELEMENT)
	{
		$attr[$i] = $reader->getAttribute("name"); // does not store anything
                $attr .= $reader->getAttribute("name"); // stores perfectly
                i++;
	}
	if ($reader->nodeType == XMLReader::TEXT)
	{
		$val[$j] = $reader->value; // performs the same as above
		$val .= $reader->value;
		$j++;
	}
}

Test script:
---------------
$reader = new XMLReader();
$url = 'http://xml.com/info.php'; // insert a real URL to test
	
$reader->open($url);
	
$i = 0;
	
while ($reader->read())
{	
        if ($reader->nodeType == XMLReader::TEXT)
	{
		$val[$i] = $reader->value; // does not work
		//$val .= $reader->value; // for demonstration only, works properly
		$i++;
	}
}

print_r($val);

Expected result:
----------------
When using a valid URL and XML sheet, "print_r" should print an array of all values stored.

Actual result:
--------------
Instead, the entire array is empty. As soon as you use .= instead of storing in an array, everything is printed fine simply using "echo".

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-18 13:20 UTC] mike@php.net
-Status: Open +Status: Feedback
 [2010-05-18 13:20 UTC] mike@php.net
Works fine here:

$ php -r '$r=new XMLReader(); $r->open("http://www.w3.org/TR/xmldsig-core/signature-example-rsa.xml"); while($r->read()){ if($r->nodeType==XMLReader::TEXT) $var[] = $r->value;} print_r($var);'
Array
(
    [0] => 60NvZvtdTB+7UnlLp/H24p7h4bs=
    [1] => 
    juS5RhJ884qoFR8flVXd/rbrSDVGn40CapgB7qeQiT+rr0NekEQ6BHhUA8dT3+BC
    TBUQI0dBjlml9lwzENXvS83zRECjzXbMRTUtVZiPZG2pqKPnL2YU3A9645UCjTXU
    +jgFumv7k78hieAGDzNci+PQ9KRmm//icT7JaYztgt4=
  
    [2] => 
          uCiukpgOaOmrq1fPUTH3CAXxuFmPjsmS4jnTKxrv0w1JKcXtJ2M3akaV1d/karvJ
          lmeao20jNy9r+/vKwibjM77F+3bIkeMEGmAIUnFciJkR+ihO7b4cTuYnEi8xHtu4
          iMn6GODBoEzqFQYdd8p4vrZBsvs44nTrS8qyyhba648=
        
    [3] => 
          AQAB
        
    [4] => 
        CN=Merlin Hughes,O=Baltimore Technologies\, Ltd.,ST=Dublin,C=IE
      
    [5] => 
          CN=Test RSA CA,O=Baltimore Technologies\, Ltd.,ST=Dublin,C=IE
        
    [6] => 970849928
    [7] => 
        MIICeDCCAeGgAwIBAgIEOd3+iDANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJJ
        RTEPMA0GA1UECBMGRHVibGluMSUwIwYDVQQKExxCYWx0aW1vcmUgVGVjaG5vbG9n
        aWVzLCBMdGQuMRQwEgYDVQQDEwtUZXN0IFJTQSBDQTAeFw0wMDEwMDYxNjMyMDda
        Fw0wMTEwMDYxNjMyMDRaMF0xCzAJBgNVBAYTAklFMQ8wDQYDVQQIEwZEdWJsaW4x
        JTAjBgNVBAoTHEJhbHRpbW9yZSBUZWNobm9sb2dpZXMsIEx0ZC4xFjAUBgNVBAMT
        DU1lcmxpbiBIdWdoZXMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALgorpKY
        Dmjpq6tXz1Ex9wgF8bhZj47JkuI50ysa79MNSSnF7SdjN2pGldXf5Gq7yZZnmqNt
        Izcva/v7ysIm4zO+xft2yJHjBBpgCFJxXIiZEfooTu2+HE7mJxIvMR7buIjJ+hjg
        waBM6hUGHXfKeL62QbL7OOJ060vKssoW2uuPAgMBAAGjRzBFMB4GA1UdEQQXMBWB
        E21lcmxpbkBiYWx0aW1vcmUuaWUwDgYDVR0PAQH/BAQDAgeAMBMGA1UdIwQMMAqA
        CEngrZIVgu03MA0GCSqGSIb3DQEBBAUAA4GBAHJu4JVq/WnXK2oqqfLWqes5vHOt
        fX/ZhCjFyDMhzslI8am62gZedwZ9IIZIwlNRMvEDQB2zds/eEBnIAQPl/yRLCLOf
        ZnbA8PXrbFP5igs3qQWScBUjZVjik748HU2sUVZOa90c0mJl2vJs/RwyLW7/uCAf
        C/I/k9xGr7fneoIW
      
)
 [2013-02-18 00:34 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 06:01:32 2024 UTC