SimpleXML question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy

    #1

    SimpleXML question

    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:

    SimpleXMLElemen t Object
    (
    [item] =SimpleXMLEleme nt Object
    (
    [@attributes] =Array
    (
    [id] =cotbed
    )

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

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

    )

    )

    )

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

    <url><title>Tes t title</title><link>htt p://tinyurl.com/ywuxc5</
    link></url>

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

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

    [urls] =SimpleXMLEleme nt Object
    (
    [url] =SimpleXMLEleme nt 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

  • Andy

    #2
    Re: SimpleXML question



    On Jan 28, 12:47 pm, "Andy" <amn...@gmail.c omwrote:
    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

    Comment

    Working...