PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #46309 JSON encode simplexml object loses attributes on leaf nodes
Submitted:16 Oct 2008 12:09am UTC Modified: 20 Oct 2008 11:10pm UTC
From:steve at stevegoodwin dot net Assigned to:
Status:Bogus Category:JSON related
Version:5.2.6 OS:Windows
View/Vote Developer Edit Submission

Welcome! If you don't have a SVN 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.
SVN Username: SVN Password:
Quick Fix:
Status: Assign to:
Category:
Summary:
From: steve at stevegoodwin dot net
New email:
Version: OS:
New/Additional Comment:

[16 Oct 2008 12:09am UTC] steve at stevegoodwin dot net
Description:
------------
If you json encode a simplexml object, any xml leaf nodes will lose 
their attributes

The attached script has two small xml strings.  One with attributes 
on the leaf node, one without.

When you run the script, attribute values disappear for the second 
json string 

Reproduce code:
---------------
<?php
$xml_string1 = '<?xml version="1.0"?><ItemList><Item
ID="456"><ExtraTag>ABC</ExtraTag></Item><Item
ID="789"><ExtraTag>DEF</ExtraTag></Item></ItemList>';
$xml_string2 = '<?xml version="1.0"?><ItemList><Item
ID="456">ABC</Item><Item ID="789">DEF</Item></ItemList>';
$xml_obj = simplexml_load_string($xml_string1);
$json = json_encode($xml_obj);
print "Input XML Document: " . htmlentities($xml_string1) .
"<br/>attributes are present:<br/>$json<br/><br/>";

$xml_obj = simplexml_load_string($xml_string2);
$json = json_encode($xml_obj);
print "Input XML Document: " . htmlentities($xml_string2) .
"<br/>attributes are <b>Not</b> present:<br/>$json<br/><br/>";
phpinfo();
?>

Expected result:
----------------
Input XML Document: <?xml version="1.0"?><ItemList><Item 
ID="456"><ExtraTag>ABC</ExtraTag></Item><Item 
ID="789"><ExtraTag>DEF</ExtraTag></Item></ItemList>

attributes are present:
{"Item":[{"@attributes":{"ID":"456"},"ExtraTag":"ABC"},{"@attributes"
:{"ID":"789"},"ExtraTag":"DEF"}]}

Input XML Document: <?xml version="1.0"?><ItemList><Item 
ID="456">ABC</Item><Item ID="789">DEF</Item></ItemList>

attributes are Not present:
{"Item":["ABC","DEF"]}

Actual result:
--------------
json string above labeled with "attributes are present:" is correctly 
json encoded.

json string above labeled with "attributes are Not present:" is 
incorrectly json encoded.  The json object does not have the 
attribute name or values

[20 Oct 2008 11:10pm UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You cannot do a lossless (loss of attributes) on an object of a certain

type such as SimpleXML.

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC