开发者

Simple XML Sort based on Name

开发者 https://www.devze.com 2023-01-29 02:03 出处:网络
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

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";
}
0

精彩评论

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

关注公众号