#1
  1. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,111
    Rep Power
    487

    SimpleXML ... not so simple!


    Hi guys
    I have taken over a more experienced PHP developer than I am and am trying to debug / work with some of their old code but am stuck on something and I thought that this might be the best place to get some assistance ....

    A remote XML file is downloaded into a variable called $myXML.
    The script makes a "foreach ($myXML -> entity as $new)" loop and within that loop, there is the following code ...

    PHP Code:
    $nm $new -> getNamespaces true ); 
    If I print_r($nm) I get the following array values:-

    (
    [] => http://www.w3.org/2005/Atom
    [m] => http://schemas.microsoft.com/ado/2007/08/dataservices/metadata
    [d] => http://schemas.microsoft.com/ado/2007/08/dataservices
    )

    I get that bit no problem but it's this line I don't quite understand what it's doing ....

    PHP Code:
        $dc $new -> content -> children $nm ['m'] ); 
    If I print_r that, I get an empty array each time.

    I'd be happy if someone can point me to a good resource on this instead of giving me the answer ... yes, I have tried Google but there's about 50 billion results and I didn't find the answer within the first three clicks (haha, I'm only joking, it was four clicks).

    I can post back with more info if you need it, but the XML file is copied below for reference.
    Many thanks for reading and I hope that someone can offer me some advice!


    Code:
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <feed xml:base="http://subdomain.example.com/Catalog/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
      <title type="text">Videos</title>
      <id>http://subdomain.example.com/Catalog/Videos</id>
      <updated>2013-06-19T11:09:25Z</updated>
      <link rel="self" title="Videos" href="Videos" />
      <entry>
        <id>http://subdomain.example.com/Catalog/Videos('AU3C71300022')</id>
        <title type="text"></title>
        <updated>2013-06-19T11:09:25Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="VideoEf" href="Videos('AU3C71300022')" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContentProviderEntity" type="application/atom+xml;type=entry" title="ContentProviderEntity" href="Videos('AU3C71300022')/ContentProviderEntity" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoVersionsCollection" type="application/atom+xml;type=feed" title="VideoVersionsCollection" href="Videos('AU3C71300022')/VideoVersionsCollection" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoBuyLinksCollection" type="application/atom+xml;type=feed" title="VideoBuyLinksCollection" href="Videos('AU3C71300022')/VideoBuyLinksCollection" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoMetadatasCollection" type="application/atom+xml;type=feed" title="VideoMetadatasCollection" href="Videos('AU3C71300022')/VideoMetadatasCollection" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoPoliciesCollection" type="application/atom+xml;type=feed" title="VideoPoliciesCollection" href="Videos('AU3C71300022')/VideoPoliciesCollection" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/GenresCollection" type="application/atom+xml;type=feed" title="GenresCollection" href="Videos('AU3C71300022')/GenresCollection" />
        <category term="example.Data.EF.Entities.VideoEf" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:Isrc>AU3C71300022</d:Isrc>
            <d:Title>Vicious Cycle</d:Title>
            <d:ThumbnailFilename>83AFECB1CECA7E9105202F9BEF5DBACD2013176124356451.jpg</d:ThumbnailFilename>
            <d:ContentProviderID m:type="Edm.Int32">1</d:ContentProviderID>
            <d:DurationInSeconds m:type="Edm.Int32">245</d:DurationInSeconds>
            <d:CreationDate m:type="Edm.DateTime">2013-06-17T17:43:56.297</d:CreationDate>
            <d:ModifiedDate m:type="Edm.DateTime">2013-06-17T17:43:56.297</d:ModifiedDate>
            <d:StartDate m:type="Edm.DateTime">2013-06-13T07:00:00</d:StartDate>
            <d:EndDate m:type="Edm.DateTime" m:null="true" />
            <d:ShowWatermark m:type="Edm.Boolean">false</d:ShowWatermark>
            <d:FavoriteCount m:type="Edm.Int64">0</d:FavoriteCount>
            <d:ViewCount m:type="Edm.Int64">605</d:ViewCount>
            <d:LastDayViewCount m:type="Edm.Int64">53</d:LastDayViewCount>
            <d:LastWeekViewCount m:type="Edm.Int64">605</d:LastWeekViewCount>
            <d:LastMonthViewCount m:type="Edm.Int64">605</d:LastMonthViewCount>
            <d:VideoYear m:type="Edm.Int32">2013</d:VideoYear>
            <d:CopyrightLine>(C) 2013 Cult Classics Pty Ltd. Manufactured and distributed exclusively by Vanguard Records, A Welk Music Group Company</d:CopyrightLine>
            <d:CommentsCount m:type="Edm.Int64">0</d:CommentsCount>
            <d:LastDayCommentsCount m:type="Edm.Int64">0</d:LastDayCommentsCount>
            <d:LastWeekCommentsCount m:type="Edm.Int64">0</d:LastWeekCommentsCount>
            <d:LastMonthCommentsCount m:type="Edm.Int64">0</d:LastMonthCommentsCount>
            <d:LastDayFavoriteCount m:type="Edm.Int64">0</d:LastDayFavoriteCount>
            <d:LastWeekFavoriteCount m:type="Edm.Int64">0</d:LastWeekFavoriteCount>
            <d:LastMonthFavoriteCount m:type="Edm.Int64">0</d:LastMonthFavoriteCount>
            <d:FilteredTitle>Vicious Cycle</d:FilteredTitle>
            <d:ThumbnailUrl>http://cache.example.com/Content/exampleImages/video/83AFECB1CECA7E9105202F9BEF5DBACD2013176124356451.jpg</d:ThumbnailUrl>
          </m:properties>
        </content>
      </entry>
     </feed>
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  2. #2
  3. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,111
    Rep Power
    487
    Hmm, I think this page --> http://php.net/manual/en/simplexmlelement.children.php

    Explains the problem (although I'm not 100% certain that I'm looking at the right thing!)

    "Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects."
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,965
    Rep Power
    9397
    PHP Code:
    $dc $new->content->children("m"true
    is much simpler than this getNamespace() and $nm stuff.

    With SimpleXMLElement objects, ->foo will look for a <foo> child of the current node in the current namespace. The "current namespace" is either the default (empty prefix - the w3.org/2005/Atom you have) or whatever was last set using children() or attributes() when accessing a parent node.
    PHP Code:
    $xml->foo /* <foo> */
         
    ->children("abc"true)->bar /* <abc:bar> */
         
    ->baz /* <abc:baz> */ 
    So if you want <m:foo> you would then call children() using either the prefix ("m") or the namespace URI (the /metadata URI). Calling it with just a prefix was added in PHP 5.2 so if you were crazy and had to write for something earlier you'd need getNamespaces().

    Comments on this post

    • badger_fruit agrees
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,111
    Rep Power
    487
    Hi requinix

    Thanks for the reply; that actually makes sense (sorry, I don't mean to sound surprised lol but when I first read your post I was like "WTF is that crazy nonsense")!

    I shall go back to the scripts with a fresher understanding and hey, might even get it fixed!!

    Cheers
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo