腾讯QQ用的国家省市数据

从QQ安装目录得到的数据文件,是xml格式,用SimpleXML解析入库很简单。

LocList.xml

$file = APP_PATH . '/../doc/LocList.xml';
$xml = simplexml_load_file($file);
$model = AbstractModel::getInstance();
$tableCountry = $model->getTable('position_country');
$tableProvince = $model->getTable('position_province');
$tableCity = $model->getTable('position_city');
foreach ($xml->children() as $child) {
    $attr = $child->attributes();
    $code = $attr['Code']->__toString();
    if ($code == 1) {
        continue;
    }
    $name = $attr->Name->__toString();
    $rowCountry = $tableCountry->fetchRow(array(
        'name=?' => $name
    ));
    if (! isset($rowCountry)) {
        $rowCountry = $tableCountry->createRow();
        $rowCountry->name = $name;
        $rowCountry->save();
    }
    foreach ($child->children() as $v1) {
        $attr = $v1->attributes();
        if (! empty($attr->Name)) {
            $stateName = $attr->Name->__toString();
        } else {
            $stateName = '全部';
        }
        $rowState = $tableProvince->fetchRow(array(
            'country_id=?' => $rowCountry->id,
            'province_name=?' => $stateName
        ));
        if (! isset($rowState)) {
            $rowState = $tableProvince->createRow();
            $rowState->country_id = $rowCountry->id;
            $rowState->province_name = $stateName;
            $rowState->save();
        }
        $rowState->province_id = $rowState->id;
        $rowState->save();
        foreach ($v1->children() as $v2) {
            $attr = $v2->attributes();
            $cityName = $attr->Name->__toString();
            $rowCity = $tableCity->fetchRow(array(
                'province_id=?' => $rowState->id,
                'city_name=?' => $cityName
            ));
            if (! isset($rowCity)) {
                $rowCity = $tableCity->createRow();
                $rowCity->province_id = $rowState->id;
                $rowCity->city_name = $cityName;
                $rowCity->save();
            }
            $rowCity->city_id = $rowCity->id;
            $rowCity->save();
        }
        echo "$namet$stateNamen";
    }
}

 

发表评论

电子邮件地址不会被公开。

CAPTCHA

*