2011-12-14 13 views

答えて

1

これは、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); 
2

ラップ{}内の文字列:

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

参照:Strings: Complex (curly) syntaxVariable variables

+0

ありがとうございます。エラーは消えていた。しかし、私がのデータを取得しようとすると、結果が得られないようです。 –

+0

これは、変数変数のレッスンが立っていますが、彼はSimpleXMLの名前空間を扱っているので(ほとんどの場合)動作しません。 –