2011-08-26 123 views
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html lang="en" dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<meta name="description" content="Players of Liverpool F.C." /> 
<meta name="keywords" content="liverpool, players of liverpool" /> 
<title>Players of Liverpool F.C.</title> 
</head> 
<body> 
<?php 
$dom = new DOMDocument(); 
$dom->loadHTMLFile('http://en.wikipedia.org/wiki/Liverpool_F.C.'); 
$domxpath = new DOMXPath($dom); 
foreach ($domxpath->query('//span[@id="Players"]/../following-sibling::table[1]//span[@class="fn"]') as $a) 
{echo 
" 
<p>$a->textContent</p> 
"; 
}; 
?> 

</body> 
</html> 

你好,我該如何解析一個包含所有$a->textContent的XML的標籤,如<player></player>PHP XML解析器問題

+0

你wan將球員列表提取到您自己的xml文檔中?或者只是將玩家粘貼到上面粘貼的這個頁面中? –

回答

1

您拼錯了維基百科文章的地址。此外,你應該把

<?xml version="1.0" encoding="UTF-8" ?> 

爲開端,在一般讓XML welformed:

<?php 
$dom = new DOMDocument(); 
$dom->loadHTMLFile('https://secure.wikimedia.org/wikipedia/en/wiki/Liverpool_fc'); 
$domxpath = new DOMXPath($dom); 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; 
echo "\t<players>\n"; 
foreach ($domxpath->query('//span[@id="Players"]/../following-sibling::table[1]//span[@class="fn"]') as $a) 
{ 
    echo "\t\t<player>$a->textContent</player>\n"; 
}; 
echo "\t</players>"; 
?> 

此輸出的玩家一個不錯的XML列表:

http://gregersboye.dk/test.php

(你可能需要看源代碼,firefox不會很好地顯示它)