开发者

PHP XML Parser Question

开发者 https://www.devze.com 2023-03-31 14:28 出处:网络
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<!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>

Hello, how can I parse an XML that includes all of the $a->textContent with a tag like <player></player>?


You had misspelled the address for the wikipedia-article. Furthermore, you should put

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

as the beginning at generally make your 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>";
?>

This output a nice xml-list of players:

http://gregersboye.dk/test.php

(you might need to look at the sourcecode, firefox doesn't display it nice as is)

0

精彩评论

暂无评论...
验证码 换一张
取 消