开发者

Passing Object Operators As Strings (PHP)

I'm building a script that takes the contents of several (~13) news feeds and parses the XML data and inserts the records into a database. Since I don't have any control over the structure of the feeds, I need to tailor an object operator for each one to drill down into the structure in order to get the information I need.

The script works just fine if the target node is one step below the root, but if my string contains a second step, it fails ( 'foo' works, but 'foo->bar' fails). I've tried escaping characters and eval(), but I feel like I'm missing something glaringly obvious. Any help would be greatly appreciated.

    // Roadmaps for xml navigation

$roadmap[1] = "deal";   // works
$roadmap[2] = "channel->item"; // fails
$roadmap[3] = "deals->deal";
$roadmap[4] = "resource"; 
$roadmap[5] = "object";
$roadmap[6] = "product"; 
$roadmap[8] = "channel->deal";
$roadmap[13] = "channel->item"; 
$roadmap[20] = "product";

$xmlSource = $xmlURL[$fID];
$xml=simplexml_l开发者_运维知识库oad_file($xmlSource) or die(mysql_error());  

if (!(empty($xml))) {
    foreach($xml->$roadmap[$fID] as $div) {

        include('./_'.$incName.'/feedVars.php');
        include('./_includes/masterCategory.php.inc');
        $test = sqlVendors($vendorName);

    } // end foreach
        echo $vUpdated." records updated.<br>";
        echo $vInserted." records Inserted.<br><br>";
} else {
    echo $xmlSource." returned an empty set!";
}  // END IF empty $xml result


While Fosco's solution will work, it is indeed very dirty.

How about using xpath instead of object properties?

$xml->xpath('deals/deal');


PHP isn't going to magically turn your string which includes -> into a second level search.

Quick and dirty hack...

eval("\$node = \"\$xml->" . $roadmap[$fID] . "\";");
foreach($node as $div) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜