这个数据结构怎么转换一下?

原本是一个xml文件:location.xml

https://img3.mukewang.com/5be4f2640001c19302640661.jpg

我想转换成这样:location.js


        location: {

            '北京': ['东城', '西城', '朝阳', '丰台', '石景山', '海淀', '门头沟', '房山', '通州', '顺义', '昌平', '大兴', '怀柔', '平谷', '密云', '延庆'],

            '天津': ['河东', '河西', '南开', '河北', '红桥', '东丽', '西青', '津南', '北辰', '武清', '宝坻', '滨海', '宁河', '静海', '蓟县']

        }

用什么方式比较好?用爬虫还是其他什么方式?

芜湖不芜
浏览 498回答 1
1回答

慕桂英546537

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

相关分类

JavaScript