For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > January 2007 > SimpleXML question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SimpleXML question
Andy

2007-01-28, 7:59 am

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
(
=> SimpleXMLElement Object

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

Andy

2007-01-28, 7:59 am



On Jan 28, 12:47 pm, "Andy" <amn...@gmail.com> wrote:

> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com