2014-12-29 3 views
0

XML 구문 분석에서 좀 새로운 기능입니다.이 코드 조각이 잘못된 이유를 알아 내려고 노력하고 있는데, 결과를 표시하지 않는 이유는 무엇입니까?PHP XML DOM 구문 분석 문제

//php code 
    $file=file_get_contents("http://".$_SERVER["HTTP_HOST"]."/sitemap.xml"); 
    $dom = new DOMDocument(); 
    $dom->loadXML($file); 
    $xmlPath = new DOMXPath($dom); 
    $arrNodes = $xmlPath->query('//loc'); 
    foreach($arrNodes as $arrNode){ 
     echo $arrNode->nodeValue; 
    } 

//sitemap.xml 
    <url> 
    <loc>http://calculosophia.com/</loc> 
    </url> 
    <url> 
     <loc>http://calculosophia.com/finance/compound-interest-calculator</loc> 
    </url> 

나는 파일이 성공적으로 검색지고 있음을 볼 수 있지만 var에 $ arrNodes를 덤프 할 때 나에게 내가 더

+0

이유 -1을 설명해주십시오. –

+0

'$ arrNode-> nodeValue'를'$ arrNode-> nodeValue'로 변경하십시오. –

+0

@RocketHazmat 아직 아무것도 없습니다 –

답변

2
$arrNodes = $xmlPath->query('//loc'); 

이 줄은 반환을 어떻게 해야할지 몰라 object(DOMNodeList)[170]을 제공합니다 0 요소를 포함하는 DOMNodeList. 루트 요소 (<urlset>)가 네임 스페이스 (xmlns 특성)를 선언하기 때문입니다. XPath는이 네임 스페이스를 사용하여 파일을 쿼리하기 전에이 네임 스페이스에 대해 알아야합니다.

$xmlPath = new DOMXPath($dom); 
// The 1st parameter is just name, it can be whatever you want 
// The 2nd parameter is the namespace URL (the value of the "xmlns" attribute) 
$xmlPath->registerNamespace('sitemap', 'http://www.sitemaps.org/schemas/sitemap/0.9'); 
$arrNodes = $xmlPath->query('//sitemap:loc'); 
+0

고마워요! 나는 PHP에서 늙었는데, 왜 나에 대해 모르겠지만이 일은 결코 내 마음에 들지 않을 것이다. 그게 어리석은가요? –