473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SimpleXML Bug?

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
7 2901
..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
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
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
..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
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: archi | last post by:
dear all, what would be the most simple way to multisort the (HTML) output from a simplexml file. imagine i have loaded a simplexml file with names & addresses & much more, and i would like...
1
by: hwcowan | last post by:
Hello, I have started using SimpleXML and can do most things, but there are a couple of things that I can't seem to figure out. Currently, I can: 1. Open & load an XML file 2. Manually...
3
by: theboss3 | last post by:
Take, for example a typical RSS 2.0 feed (like http://lorelle.wordpress.com/feed/) and tell me how I access the tags like 'content:encoded' and 'wfw:commentRSS' using SimpleXML. I've searched far...
1
by: root | last post by:
Hi folks - hope someone can help me. Firstly, my apologies for crossposting this to alt.php (and not properly crossposting either). I have an XML file with some elements like those below ...
2
by: mandric | last post by:
Hello, Can someone please enlighten me on how to preserve the <!]> element when parsing an xml file or string with simplexml. I'm using libxml 2.6.16 and php 5.1.4. I tried a few variations,...
2
by: whisher | last post by:
Hi. I'm taking my first steps with xml and SimpleXML functions. xml.php (It could be made dynamically with a mysql result I post this snippet for example ) PHP Code xml.php: <?php $xmlstr...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
3
by: bleen | last post by:
I'm trying to use SimpleXML but I've run into a conundrum. Every day an XML file is generated that this script grabs and manipulates. How can I check that the XML has no problems before creating...
0
by: ty | last post by:
I have a script that takes a xml template then adds data into it using SimpleXMLElement. I then save it using asXML into a mysql database. On my development machine at home it works fine. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.