PHP XML分析器问题

时间:2011-08-26 15:21:30

标签: php xml regex

<!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标记的<player></player>的XML?

1 个答案:

答案 0 :(得分:1)

您拼错了维基百科文章的地址。此外,你应该把

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

作为一般开始使你的xml健康:

<?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不会显示它很好)

相关问题