Parsing XML with SimpleXML

Discussion in 'PHP' started by WPPJames, May 3, 2010.

  1. #1
    I'm attempting to use php and SimpleXML to assign variables from an XML output. This works:

    $xml = simplexml_load_file($request_url) or die("feed not loading");

    and when I do a:

    echo "<pre>";
    var_dump($xml);
    echo "</pre>";

    I get one of these two results depending on value of "Type" attribute.


    object(SimpleXMLElement)#1 (2) {
    ["@attributes"]=>
    array(1) {
    ["Type"]=>
    string(5) "Popup"
    }
    ["ad"]=>
    object(SimpleXMLElement)#2 (6) {
    ["url"]=>
    object(SimpleXMLElement)#3 (0) {
    }
    ["cpvrate"]=>
    string(5) "0.005"
    ["creativeid"]=>
    string(10) "0204499385"
    ["campaignid"]=>
    string(12) "2-0210728303"
    ["max-imp-per-user"]=>
    string(1) "1"
    ["frequency-period"]=>
    string(2) "24"
    }
    }


    object(SimpleXMLElement)#1 (1) {
    ["@attributes"]=>
    array(1) {
    ["Type"]=>
    string(7) "NoPopup"
    }
    }


    In my php code I'm assigning variables like this:

    $adtype = (string) $xml->Type;
    $outurl = (string) $xml->ad->url;
    $cpvr = (string) $xml->ad->cpv_rate;
    $crid = (string) $xml->ad->creativeid;
    $caid = (string) $xml->ad->campaignid;
    $mipu = (string) $xml->ad->max-imp-per-user;
    $fp = (string) $xml->ad->frequency-period;

    Only $outurl gets assigned. The other variables remain "empty". What should the correct syntax be for the other variable assignments?
     
    Last edited: May 3, 2010
    WPPJames, May 3, 2010 IP