By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,979 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,979 IT Pros & Developers. It's quick & easy.

SimpleXML Bug?

P: n/a
So I'm trying to use SimpleXML to get some attribute information about
some nodes in my XML document, but it seems like SimpleXML ignores
attributes for elements with no children,

For instance:

<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>

I can get the attribute foo from the object element, but there does
not seem to be any way to retrieve the mode attrbitue from the color
element. print_r() of this data shows that mode="rgb" is totally
lost.

If this is a bug, I'll go report this to php.net, but if it's not,
could someone explain either the reasoning behind it or how to get
this information?
Jun 2 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
..oO(dimo414)
>So I'm trying to use SimpleXML to get some attribute information about
some nodes in my XML document, but it seems like SimpleXML ignores
attributes for elements with no children,

For instance:

<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>

I can get the attribute foo from the object element, but there does
not seem to be any way to retrieve the mode attrbitue from the color
element. print_r() of this data shows that mode="rgb" is totally
lost.
Pleast post your PHP code.

Micha
Jun 2 '08 #2

P: n/a
On May 23, 2:07 am, Michael Fesser <neti...@gmx.dewrote:
.oO(dimo414)
So I'm trying to use SimpleXML to get some attribute information about
some nodes in my XML document, but it seems like SimpleXML ignores
attributes for elements with no children,
For instance:
<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>
I can get the attribute foo from the object element, but there does
not seem to be any way to retrieve the mode attrbitue from the color
element. print_r() of this data shows that mode="rgb" is totally
lost.

Pleast post your PHP code.

Micha
Well, if I print_r the simplexml object generated by my example xml, I
get the following:
SimpleXMLElement Object
(
[object] =SimpleXMLElement Object
(
[@attributes] =Array
(
[foo] =bar
)
[color] =ffddee
)
)
Note that the mode attribute is completely lost.

If you really need to see the php code that generated this, here it
is:
<?php

$xml = '<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>';

$simple = simplexml_load_string($xml);

print_r($simple);

?>
Jun 2 '08 #3

P: n/a
dimo414 escribió:
<?php

$xml = '<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>';

$simple = simplexml_load_string($xml);

print_r($simple);

?>
The issue is probably related to print_r():

<?php

$xml = '<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>';

$simple = simplexml_load_string($xml);

// Applied here, it does print the right value:
print_r($simple->object[0]->color[0]['mode']);

?>

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #4

P: n/a
..oO(dimo414)
>Well, if I print_r the simplexml object generated by my example xml, I
get the following:
SimpleXMLElement Object
(
[object] =SimpleXMLElement Object
(
[@attributes] =Array
(
[foo] =bar
)
[color] =ffddee
)
)
Note that the mode attribute is completely lost.

If you really need to see the php code that generated this, here it
is:
<?php

$xml = '<xmlfile>
<object foo="bar">
<color mode="rgb">ffddee</color>
</object>
</xmlfile>';

$simple = simplexml_load_string($xml);

print_r($simple);

?>
Ah, I missed the print_r() from your first posting. Well, just printing
out a SimpleXML object usually doesn't work as expected. It's simply
because of how these objects are handled and created internally. You
would run into the same problems if you want to print out an instance of
a class which makes heavy use of the __set() and __get() interceptor
methods to access their properties - a simple print_r() won't work.

Anyway, you can still use XPath or the SimpleXML syntax as described in
the manual to access all the nodes and attributes:

print_r($simple);
print_r($simple->object);
print_r($simple->object->color['mode']);

Sometimes it might also be necessary to explicitly cast a node to string
if you want to work with it.

Micha
Jun 2 '08 #5

P: n/a
Michael Fesser wrote:
.oO(dimo414)

>Well, if I print_r the simplexml object generated by my example xml, I
get the following:
SimpleXMLElement Object
(
[object] =SimpleXMLElement Object
(
[@attributes] =Array
(
[foo] =bar
)
[color] =ffddee
)
)

Maybe I shouldn't use this thread to ask this question , but ......

I'm confused by the '@' notation on @attributes in a SimpleXMLElement
Object.

I can find no documentation explaining what it means or how it got there.

Can anyone explain? And how would I create a member like this in an
object I create?

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Nothing he's got he really needs
Twenty first century schizoid man.
***********************************

Jun 2 '08 #6

P: n/a
..oO(Chuck Anderson)
>Maybe I shouldn't use this thread to ask this question , but ......

I'm confused by the '@' notation on @attributes in a SimpleXMLElement
Object.

I can find no documentation explaining what it means or how it got there.
I may be wrong, but I don't think it has any special meaning. Remember
that SimpleXML is written in C and not limited to what the PHP parser
accepts. Since '@attributes' only seem to appear once in the SimpleXML
source code, I think it's just a naming convention, perhaps following
the XPath syntax for accessing attributes.
>Can anyone explain? And how would I create a member like this in an
object I create?
Not directly, because the parser wouldn't allow it. But there are other
ways to define and access class members of almost arbitrary names if you
have to, e.g.

<?php
$foo = new StdClass();
$bar = '@attributes';
$foo->$bar = 'something';
var_dump($foo);
?>

Micha
Jun 2 '08 #7

P: n/a
Michael Fesser wrote:
.oO(Chuck Anderson)

>Maybe I shouldn't use this thread to ask this question , but ......

I'm confused by the '@' notation on @attributes in a SimpleXMLElement
Object.

I can find no documentation explaining what it means or how it got there.

I may be wrong, but I don't think it has any special meaning. Remember
that SimpleXML is written in C and not limited to what the PHP parser
accepts. Since '@attributes' only seem to appear once in the SimpleXML
source code, I think it's just a naming convention, perhaps following
the XPath syntax for accessing attributes.

>Can anyone explain? And how would I create a member like this in an
object I create?

Not directly, because the parser wouldn't allow it. But there are other
ways to define and access class members of almost arbitrary names if you
have to, e.g.

<?php
$foo = new StdClass();
$bar = '@attributes';
$foo->$bar = 'something';
var_dump($foo);
?>

Micha
Okay, thanks for the explanation. What confused me is when using
SimpleXML, I can not access "@attributes" like the other object members.

When I do a print_r of a SimpleXMLElement Object, attributes are listed
(along with children) this way:

[node1] SimpleXMLElement Object
(
[@attributes] =array
(
['attribname'] =attribvalue
)
[child1] =child1value
[child2] =child2value
}

I only can access the value of the attribute "attribname" by either:
$value = node1['attribname']
.... which bypasses the array it appears to be in (@attributes). ??

or

$attribs = node1->attributes();
$value = $attribs['attribname']
.... using the object method, attributes().

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Nothing he's got he really needs
Twenty first century schizoid man.
***********************************

Jun 2 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.