我使用 Simplexml 和 xpath 来解析我的 XML。
这是我的 xml 输出:
<?xml version="1.0"?>
<api>
<query>
<categorymembers>
<cm pageid="30908" ns="0" title="Thomas Acda" />
<cm pageid="46896" ns="0" title="Elvan Akyildiz" />
<cm pageid="21162" ns="0" title="Jenny Arean" />
<cm pageid="30913" ns="0" title="Tijl Beckand" />
<cm pageid="829" ns="0" title="Yoka Berretty" />
<cm pageid="45" ns="0" title="Wim de Bie" />
<cm pageid="31894" ns="0" title="Jan Blaaser" />
<cm pageid="832" ns="0" title="Karin Bloemen" />
<cm pageid="833" ns="0" title="Hetty Blok" />
<cm pageid="12473" ns="0" title="Ron Boszhard" />
<cm pageid="34605" ns="0" title="Ansje van Brandenberg" />
<cm pageid="8451" ns="0" title="Claudia de Breij" />
<cm pageid="678" ns="0" title="Jos Brink" />
<cm pageid="29595" ns="0" title="Joke Bruys" />
<cm pageid="30909" ns="0" title="Horace Cohen" />
<cm pageid="689" ns="0" title="Gerard Cox" />
<cm pageid="32008" ns="0" title="Pieke Dassen" />
<cm pageid="31047" ns="0" title="Wieteke van Dort" />
</categorymembers>
</query>
</api>
这是我显示标题的代码。
<?php
$xml = new SimpleXMLElement('http://www.beeldengeluidwiki.nl/api.php?action=query&list=categorymembers&cmtitle=Category:Cabaretier&cmlimit=400', null, true);
$data = $xml->xpath("/api/query/categorymembers/cm");
foreach ($data as $item) {
echo $item['title'];
}
?>