Simple XML Sort based on Name
I am trying to sort a list of databases and documents in each database by the databasename. Then print them out in a tabled format
here is the xml
<responsedata>
<databases>
<database>
<name>Test</name>
<documents>0</documents>
</database>
−
<database>
<name>Test2</name>
<documents>0</documents>
</database>
</databases>
</responsedata>
here is my current code that lists the databases and d开发者_如何学Pythonocuments without the sort and print them out in a list.
$request_url = "http://255.255.255.255/xmlfile";
$xml = simplexml_load_file($request_url)
foreach ($xml->responsedata->databases->database as $db) {
echo"<tr><td>",$db->name,"</td><td>",$db->documents,"</td></tr>\n";
}
//
$dbs = array();
foreach ($xml->responsedata->databases->database as $db) {
$dbs[(string)$db->name] = $db;
}
ksort($dbs);
foreach ($dbs as $db) {
echo"<tr><td>",htmlentities($db->name),"</td>",
"<td>",htmlentities($db->documents),"</td></tr>\n";
}
精彩评论