php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51154 SimpleXMLElement does not support dash in nodename
Submitted: 2010-02-26 08:38 UTC Modified: 2010-02-26 09:25 UTC
From: mr at inpay dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.1 OS: Windows 7 Professional
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: mr at inpay dot com
New email:
PHP Version: OS:

 

 [2010-02-26 08:38 UTC] mr at inpay dot com
Description:
------------
Having a XML string, where one or more of the nodenames contains a - 
(dash) it is impossible to return that value, as dash is not allowed in 
attributes on an object. Consider replacing all dash'es with 
underscores.

Reproduce code:
---------------
<?php
	$xml_string = "<response><http-code>200</http-code></response>";
	$xml = new SimpleXMLElement($xml_string);
	
	print_r($xml);
?>

Expected result:
----------------
SimpleXMLElement Object ( [http-code] => 200 )

Actual result:
--------------
SimpleXMLElement Object ( [http_code] => 200 )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-26 08:40 UTC] mr at inpay dot com
Oops, but accident i switched the "Expected result" and "Actual result"

Expected result should be SimpleXMLElement Object ( [http_code] => 200 )
Actual result should be SimpleXMLElement Object ( [http-code] => 200 )

Sorry for the inconvinience.
 [2010-02-26 09:14 UTC] aharvey@php.net
This works fine, so long as you use the right syntax. Example 3 at 
http://au2.php.net/manual/en/simplexml.examples-basic.php covers this.

In your case, you would access http-code as follows:

$httpCode = $xml->{'http-code'};
 [2010-02-26 09:25 UTC] mr at inpay dot com
Ohh, thanks alot. Wasn't aware of that syntax.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 08:01:28 2025 UTC