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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC