2012-09-07 7 views
0

이것에 대한 몇 가지 다른 게시물을 살펴 보았지만 기쁨은 없습니다.PHP에서 XML 속성 얻기

이 코드있어 :

$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml"; 
$string = file_get_contents($url); 
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string); 
$xml = simplexml_load_string($string); 

foreach ($xml->entry as $val) { 
    echo "RESULTS: " . $val->attributes() . "\n"; 

을하지만 난 어떤 결과를 얻을 수 없습니다.

<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&amp;uo=2</id> 

어떤 제안 : 나는이 조각에 549,592,189 될 것 ID 값을 얻기에 특히 관심이 있어요? xpath

+1

정규식가 무엇입니까? – Herbert

+0

regexes로 html/xml을 조작하지 마십시오. 너는 단지 곤경에 구걸 중이다. –

+0

[구문 분석 Html Cthulhu 방법] (http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhuway.html)을 읽어야합니다. 그것은 html에 관한 것이지만 xml도 적용됩니다. – ShadowScripter

답변

0

SimpleXML을주는 당신이 쉬운 방법은 XML 구조로 드릴 다운하고 원하는 요소 (들)을 얻을 수있다. 그것이 무엇이든, 정규 표현식에 대한 필요가 없습니다.

<?php 

// Load XML 
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml"; 
$string = file_get_contents($url); 
$xml = new SimpleXMLElement($string); 

// Get the entries 
$entries = $xml->entry; 

foreach($entries as $e){ 
    // Get each entriy's id 
    $id = $e->id; 
    // Get the attributes 
    // ID is in the "im" namespace 
    $attr = $id->attributes('im', TRUE); 
    // echo id 
    echo $attr['id'].'<br/>'; 
} 

DEMO : http://codepad.viper-7.com/qNo7gs

0

시도 :

$doc  = new DOMDocument; 
@$doc->loadHTML($string); 
$xpath = new DOMXpath($doc); 
$r  = $xpath->query("//id/@im:id"); 
$id  = $r->item(0)->value; 
+0

@ $ 의미는 무엇입니까? – Snowcrash

+0

@ snow-crash [오류 제어 운영자] (http://php.net/manual/en/language.operators.errorcontrol.php) * "PHP의 표현식 앞부분에 오류 메시지가 생성되면 표현은 무시됩니다. "*이 경우에는 조작 된 다운로드 문서를 통한 PHP 경고를 무시하는 데 도움이됩니다. –

0

시도 :

$sxml = new SimpleXMLElement($url); 
for($i = 0;$i <=10;$i++){ 
$appid= $sxml->entry[$i]->id->attributes("im",TRUE); 
echo $appid; 
}