1 回答
TA贡献1848条经验 获得超10个赞
本地有PHP环境的话可以用这个转换
<?php
$source = <<<XML
<Location>
<CountryRegion Name="中国" Code="1">
<State Name="北京" Code="11">
<City Name="东城" Code="1" />
<City Name="西城" Code="2" />
<City Name="朝阳" Code="5" />
<City Name="丰台" Code="6" />
<City Name="石景山" Code="7" />
<City Name="海淀" Code="8" />
<City Name="门头沟" Code="9" />
<City Name="房山" Code="11" />
<City Name="通州" Code="12" />
<City Name="顺义" Code="13" />
<City Name="昌平" Code="21" />
<City Name="大兴" Code="24" />
<City Name="平谷" Code="26" />
<City Name="怀柔" Code="27" />
<City Name="密云" Code="28" />
<City Name="延庆" Code="29" />
</State>
<State Name="天津" Code="12">
<City Name="和平" Code="1" />
<City Name="河东" Code="2" />
<City Name="河西" Code="3" />
<City Name="南开" Code="4" />
<City Name="河北" Code="5" />
<City Name="红桥" Code="6" />
<City Name="滨海新区" Code="26" />
<City Name="东丽" Code="10" />
<City Name="西青" Code="11" />
<City Name="津南" Code="12" />
<City Name="北辰" Code="13" />
<City Name="宁河" Code="21" />
<City Name="武清" Code="22" />
<City Name="静海" Code="23" />
<City Name="宝坻" Code="24" />
<City Name="蓟县" Code="25" />
</State>
</CountryRegion>
</Location>
XML;
$result = [
'location' => [],
];
$xml = simplexml_load_string($source);
foreach ($xml->CountryRegion->State as $state) {
$province = '';
foreach ($state->attributes() as $key => $att) {
if ($key == 'Name') {
$province = (string) $att;
}
}
$result['location'][$province] = [];
foreach ($state->City as $city) {
foreach ($city->attributes() as $key => $attr) {
if ($key == 'Name') {
$result['location'][$province][] = (string) $attr;
}
}
}
}
echo (json_encode($result, JSON_UNESCAPED_UNICODE));
保存为test.php,然后执行php test.php
即可。输出如下
{"location":{"北京":["东城","西城","朝阳","丰台","石景山","海淀","门头沟","房山","通州","顺义","昌平","大兴","平谷","怀柔","密云","延庆"],"天津":["和平","河东","河西","南开","河北","红桥","滨海新区","东丽","西青","津南","北辰","宁河","武清","静海","宝坻","蓟县"]}}
添加回答
举报