<?php
header("content-type:text/xml;charset=utf-8");
include 'pdo.class.php'; //包含 pdo 类
$PdoMySQL=new PdoMySQL(); // 实例化 pdo 类
$data = $PdoMySQL->find('region'); //查询 region 表数据
// var_dump($data);exit;
$region = get_tree($data); //获取递归
/** 递归 */
function get_tree($data,$pid = 1)
{
$region = array();
foreach ($data as $key => $value)
{
if($value['parent_id'] == $pid)
{
$region[$key] = $value;
$region[$key]['region'] = get_tree($data,$value['region_id']);
}
}
return $region;
}
/**----- 创建 xml -----*/
$dom = new DOMDocument('1.0','utf8');
//创建根节点
$root = $dom -> createElement('root');
//创建国家节点
$country = $dom -> createElement('country');
$country -> setAttribute('name','country');
//外层循环 省
foreach ($region as $key => $value)
{
//创建省 属性节点
$province = $dom -> createElement('province');
$province -> setAttribute('name',$value['region_name']);
$country -> appendChild($province);
//循环市
foreach ($value['region'] as $ke => $val)
{
//创建市 属性节点
$city = $dom -> createElement('city');
$city -> setAttribute('name',$val['region_name']);
$province -> appendChild($city);
foreach ($val['region'] as $k => $v)
{
//创建区 属性节点
$area = $dom -> createElement('area');
$area -> setAttribute('name',$v['region_name']);
$city -> appendChild($area);
}
}
}
$root -> appendChild($country);
$dom -> appendChild($root);
// $dom -> save('./region.xml');
echo $dom->saveXML($dom);
?>