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

SimpleXML question

P: n/a
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 should be
resolved. I'm actually only just starting to use SimpleXML with any
vigor, so I may well be mistaken about what I think it should do...

Basically, if you take this simple chunk of XML:

<?xml version="1.0"?>
<items>
<item id="cotbed">
<name>Cot-bed</name>
<uk price="200">
<urls>
<url title="Test title">http://tinyurl.com/ywuxc5</
url>
</urls>
</uk>
</item>
</items>

The 'url' isn't converted in to an object so you cannot access the
'title' parameter - it's just basically a string as can be seen by
this simple print_r output:

SimpleXMLElement Object
(
[item] =SimpleXMLElement Object
(
[@attributes] =Array
(
[id] =cotbed
)

[name] =Cot-bed
[uk] =SimpleXMLElement Object
(
[@attributes] =Array
(
[price] =200
)

[urls] =SimpleXMLElement Object
(
[url] =http://tinyurl.com/ywuxc5
)

)

)

)

Of course, if I made the url line look like this:

<url><title>Test title</title><link>http://tinyurl.com/ywuxc5</
link></url>

then it's naturally turned in to an object and gets the attribute:

[uk] =SimpleXMLElement Object
(
[@attributes] =Array
(
[price] =200
)

[urls] =SimpleXMLElement Object
(
[url] =SimpleXMLElement Object
(
[title] =Test title
[link] =http://tinyurl.com/
ywuxc5
)

)

)

Am I wrong in thinking that the former format of url should get
converted to an object?

I'm calling simplexml_load_file with only one parameter, the file
name.

Cheers,

Andy

Jan 28 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a


On Jan 28, 12:47 pm, "Andy" <amn...@gmail.comwrote:
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 should be
resolved. I'm actually only just starting to use SimpleXML with any
vigor, so I may well be mistaken about what I think it should do...
[snip]

Well, apparently I am mistaken (no surprise there!)

Even though the print_r output doesn't show the title attribute,
playing around with the asXML() method showed that the parameter was
actually there. So trying to drill down further with:

$url = (string)$xml->item[0]->uk->urls->url[0]['title'];
echo $url;

does indeed get me the title of the url.

I guess that just goes to show that I need to give an issue a couple
more minutes thought before posting... ;-)

Andy

Jan 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.