2011-12-14 3 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의 네임 스페이스를 다루고 있기 때문에 (거의 맞지 않습니다) 작동하지 않습니다. –

관련 문제