如果我输入http://localhost/sitename?filter=city,我想以xml格式显示所有城市
我希望像这样的标签格式显示xml
<city>
<navi mumbai>
<pune>
</city>
答案 0 :(得分:0)
从你输出我假设你想要这样的东西:
<?php
if (isset($_GET["filter"]))
{
$filter = $_GET["filter"];
// create doctype
$dom = new DOMDocument("1.0");
switch($filter)
{
case "city":
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("cities");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("city1");
$root->appendChild($item);
$item = $dom->createElement("city2");
$root->appendChild($item);
break;
default:
// do something else
}
// save and display tree
echo $dom->saveXML();
}
?>
请注意,您无法将XML元素的名称与空格分开,就像您在示例中所做的那样。
答案 1 :(得分:-1)
以下是示例php代码,运行此代码并将您的链接作为过滤器作为您的条件
Header("Content-type: text/xml");
// get query string params
$filter = $_GET['filter'];
$xml = '';
If ($filter == "city"){
$xml = $xml . '<continent name="city">';
$xml = $xml . '<city id="1">Navi Mumbai</city>';
$xml = $xml . '<city id="2">Thane</city>';
$xml = $xml . '<city id="3">Pune</city>';
$xml = $xml . '</continent>';
}else If ($filter == "country"){
$xml = $xml . '<continent name="country">';
$xml = $xml . '<country id="100">India</country>';
$xml = $xml . '</continent>';
}else If ($filter == "state"){
$xml = $xml . '<continent name="state">';
$xml = $xml . '<state id="300">Maharastra</state>';
$xml = $xml . '</continent>';
}else{
$xml = $xml . '<continent name="none">';
$xml = $xml . '<country id="0">no result found</country>';
$xml = $xml . '</continent>';
}
// send xml to client
echo( $xml );
?>