阅读XML节点:

问题描述:

我有一个Feed,但有一个:在我想要检索的值的项目中。我该怎么办?阅读XML节点:

饲料:http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100

foreach($xml->entry as $game) {  
    $it = $it+1; 
    $name = mysql_real_escape_string($game->title);   
    $link = $game->link[href]; 
    $description = mysql_real_escape_string($game->media:description); 

这就是所谓的XML命名空间的前缀。 See this namespace tutorial

实际上,您并不匹配前缀,而是匹配前缀所代表的名称空间。

你如何做到这一点完全取决于你用什么来操纵XML。你不会说你在用什么,但我会猜测你正在使用SimpleXML。

对于SimpleXML,默认情况下,对象访问树中只包含没有名称空间的节点。要获得命名空间的元素,你需要去询问他们:

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100'); 

foreach($xml->entry as $game) { 
    $description = (string) $game->children('http://search.yahoo.com/mrss/')->description; 
    var_dump($description); 
} 

虽然它可能不是最好的选择,在这种特殊情况下,也可以使用XPath命名空间节点更直接匹配:

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100'); 

$NS = array(
    'media' => 'http://search.yahoo.com/mrss/', 
); 
foreach ($NS as $prefix => $uri) { 
    $xml->registerXPathNamespace($prefix, $uri); 
} 

foreach($xml->entry as $entry) { 
    // match the first media:description element 
    // get the first SimpleXMLElement in the match array with current() 
    // then coerce to string. 
    $description = (string) current($entry->xpath('media:description[1]')); 
    var_dump($description); 
} 

下面是一个更完整的例子,它也让你的代码稍微有些变了。

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100'); 

// This gets all the namespaces declared in the root element 
// using the prefix as declared in the document, for convenience. 
// Note that prefixes are arbitrary! So unless you're confident they 
// won't change you should not use this shortcut 
$NS = $xml->getDocNamespaces(); 

$games = array(); 
foreach($xml->entry as $entry) { 
    $mediaentry = $entry->children($NS['media']); 
    $games[] = array(
     // to get the text value of an element in SimpleXML, you need 
     // explicit cast to string 
     'name' => (string) $entry->title, 
     // DO NOT EVER use array-access brackets [] without quoting the string in them! 
     // I.e., don't do "$array[name]", do "$array['name']" 
     // This is a PHP error that happens to work. 
     // PHP looks for a *CONSTANT* named HREF, and replaces it with 
     // string 'href' if it doesn't find one. This means your code will break 
     // if define('href') is ever used!! 
     'link' => (string) $entry->link['href'], 
     'description' => (string) $mediaentry->description, 
    ); 
} 
$it = count($games); // there is no need for your $it+1 counter! 

// $games now has all your data. 
// If you want to insert into a database, use PDO if possible and prepare a query 
// so you don't need a separate escaping step. 
// If you can't use PDO then do: 
// $escapedgame = array_map('mysql_real_escape_string', $thegame); 

裹在{}字符串:

$description = mysql_real_escape_string($game->{'media:description'}); 

见:Strings: Complex (curly) syntaxVariable variables

+0

感谢您的帮助。错误消失了。但是,当我尝试获取中的数据时,我似乎没有得到任何结果。 – 2011-12-14 23:49:39