背景
由于本人编写的Mobile Radio项目电台信息以XML格式来保存,那些电台配置信息来自于 www.1radio.com.au 网站,该网站把配置数据保存为Json格式,所以编写一个JavaScript程序把这些配置数据信息从Json转换为XML格式。
Mobile Radio项目可以参考:
Windows Mobile和Wince下的WTL(Windows Template Library)开发
Windows Mobile 和 Wince 下的 WTL(Windows Template Library) 界面开发
Windows Mobile和Wince下使用WTL进行Windows Media Player开发
简介
Json数据由于其轻装,易读和方便操作等优点已经被广泛应用于网络服务数据传输。本文从一个实例讲述如何把Json数据转换成XML格式。使用了JavaScript实现。
实现
在 郭靖,winter-cn 和 Tim 同学的帮助下使用JavaScript来实现。
Json数据
下面为来自于 www.1radio.com.au 网站的原Json数据,保存电台信息。关于Json的介绍可以看http://json.org/
Code
var stations = [
{sid:1,country:'au',state:'NSW',city:'Sydney',logname:'2DAY',name:'2 Day FM',alttag:'2DayFM',website:'http://www.2dayfm.com.au',image:'NSW_2DAY.gif',stream:'http://resources.2dayfm.com.au/listenfeed/2dayfm.asx',locid:1,urlname:'2_day_fm'},
{sid:3,country:'au',state:'NSW',city:'Sydney',logname:'2UE',name:'2UE Talkback Radio',alttag:'2UE Talkback Radio',website:'http://www.2ue.com.au',image:'NSW_2UE.gif',stream:'http://streaming.mytalk.com.au/2ue',locid:1,urlname:'2ue_talkback_radio'},
{sid:58,country:'au',state:'QLD',city:'Brisbane',logname:'4BC',name:'4BC',alttag:'4BC',website:'http://www.4bc.com.au',image:'QLD_4BC.gif',stream:'http://streaming.mytalk.com.au/4bc',locid:4,urlname:'4bc'},
{sid:59,country:'au',state:'QLD',city:'Brisbane',logname:'4BH',name:'4BH',alttag:'4BH',website:'http://www.4bh.com.au/',image:'QLD_4BH.gif',stream:'http://streaming.mytalk.com.au/4bh',locid:4,urlname:'4bh'},
{sid:57,country:'au',state:'QLD',city:'Brisbane',logname:'4KQ',name:'4KQ',alttag:'4KQ Classic Hits',website:'http://www.4kq.com.au/',image:'QLD_4KQ.gif',stream:'http://www.4kq.com.au/media/4kq/streaming/4kq.asx',locid:4,urlname:'4kq'},
{sid:43,country:'au',state:'SA',city:'Adelaide',logname:'5AA',name:'5AA',alttag:'5AA',website:'http://www.5aa.com.au',image:'SA_5AA.gif',stream:'http://www.fiveaa.com.au/dmgcustom/fiveaa.asx',locid:5,urlname:'5aa'},
{sid:33,country:'au',state:'WA',city:'Perth',logname:'6PR',name:'882 6PR',alttag:'882 6PR',website:'http://www.6pr.com.au',image:'WA_6PR.gif',stream:'http://streaming.mytalk.com.au/6pr',locid:3,urlname:'882_6pr'},
{sid:30,country:'au',state:'WA',city:'Perth',logname:'6PPM',name:'92.9',alttag:'92.9',website:'http://www.929.com.au',image:'WA_929.gif',stream:'http://resources.929.com.au/listenfeed/929.asx',locid:3,urlname:'92.9'},
{sid:32,country:'au',state:'WA',city:'Perth',logname:'6NOW',name:'96FM',alttag:'96FM',website:'http://www.96fm.com.au',image:'WA_96FM.gif',stream:'http://streaming.amnet.net.au/96fm',locid:3,urlname:'96fm'},
{sid:56,country:'au',state:'QLD',city:'Brisbane',logname:'4BFM',name:'97.3 FM',alttag:'97.3 FM',website:'http://www.973fm.com.au/',image:'QLD_973FM.gif',stream:'http://208.96.14.149/973fm',locid:4,urlname:'97.3_fm'},
{sid:25,country:'au',state:'NSW',city:'Sydney',logname:'2BL',name:'ABC 702 Sydney',alttag:'ABC 702 Sydney',website:'http://www.abc.net.au/sydney',image:'NSW_ABC702.gif',stream:'http://abc.net.au/streaming/702/702stream.asx',locid:1,urlname:'abc_702_sydney'},
{sid:35,country:'au',state:'WA',city:'Perth',logname:'6WF',name:'ABC 720 Perth',alttag:'ABC 720 Perth',website:'http://www.abc.net.au/perth',image:'WA_ABC720.gif',stream:'http://abc.net.au/perth/onair/720stream.asx',locid:3,urlname:'abc_720_perth'},
{sid:48,country:'au',state:'SA',city:'Adelaide',logname:'5WF',name:'ABC 891 Adelaide',alttag:'ABC 891 Adelaide',website:'http://www.abc.net.au/adelaide/',image:'SA_ABC720.gif',stream:'http://www.abc.net.au/adelaide/onair/891stream.asx',locid:5,urlname:'abc_891_adelaide'},
{sid:38,country:'au',state:'WA',city:'Perth',logname:'6BACCF',name:'ABC Classic FM',alttag:'ABC Classic FM',website:'http://www.abc.net.au/classic',image:'ABCCFM.gif',stream:'http://www.abc.net.au/streaming/classic/classicfm.asx',locid:3,urlname:'abc_classic_fm'},
{sid:28,country:'au',state:'NSW',city:'Sydney',logname:'2BACCF',name:'ABC Classic FM',alttag:'ABC Classic FM',website:'http://www.abc.net.au/classic',image:'ABCCFM.gif',stream:'http://www.abc.net.au/streaming/classic/classicfm.asx',locid:1,urlname:'abc_classic_fm'},
{sid:51,country:'au',state:'SA',city:'Adelaide',logname:'5BACCF',name:'ABC Classic FM',alttag:'ABC Classic FM',website:'http://www.abc.net.au/classic',image:'ABCCFM.gif',stream:'http://www.abc.net.au/streaming/classic/classicfm.asx',locid:5,urlname:'abc_classic_fm'},
{sid:63,country:'au',state:'QLD',city:'Brisbane',logname:'4BACCF',name:'ABC Classic FM',alttag:'ABC Classic FM',website:'http://www.abc.net.au/classic',image:'ABCCFM.gif',stream:'http://www.abc.net.au/streaming/classic/classicfm.asx',locid:4,urlname:'abc_classic_fm'},
{sid:36,country:'au',state:'WA',city:'Perth',logname:'6PB',name:'ABC News Radio',alttag:'ABC News Radio',website:'http://www.abc.net.au/newsradio',image:'ABCNR.gif',stream:'http://www.abc.net.au/streaming/newsradio.asx',locid:3,urlname:'abc_news_radio'},
{sid:26,country:'au',state:'NSW',city:'Sydney',logname:'2ABCNR',name:'ABC News Radio',alttag:'ABC News Radio',website:'http://www.abc.net.au/newsradio',image:'ABCNR.gif',stream:'http://www.abc.net.au/streaming/newsradio.asx',locid:1,urlname:'abc_news_radio'},
{sid:49,country:'au',state:'SA',city:'Adelaide',logname:'5PB',name:'ABC News Radio',alttag:'ABC News Radio',website:'http://www.abc.net.au/newsradio',image:'ABCNR.gif',stream:'http://www.abc.net.au/streaming/newsradio.asx',locid:5,urlname:'abc_news_radio'},
{sid:61,country:'au',state:'QLD',city:'Brisbane',logname:'4PB',name:'ABC News Radio',alttag:'ABC News Radio',website:'http://www.abc.net.au/newsradio',image:'ABCNR.gif',stream:'http://www.abc.net.au/streaming/newsradio.asx',locid:4,urlname:'abc_news_radio'},
{sid:37,country:'au',state:'WA',city:'Perth',logname:'6RN',name:'ABC Radio National',alttag:'ABC Radio National',website:'http://www.abc.net.au/rn',image:'ABCRN.gif',stream:'http://www.abc.net.au/streaming/RN.asx',locid:3,urlname:'abc_radio_national'},
{sid:27,country:'au',state:'NSW',city:'Sydney',logname:'2ABCRN',name:'ABC Radio National',alttag:'ABC Radio National',website:'http://www.abc.net.au/rn',image:'ABCRN.gif',stream:'http://www.abc.net.au/streaming/RN.asx',locid:1,urlname:'abc_radio_national'},
{sid:50,country:'au',state:'SA',city:'Adelaide',logname:'5RN',name:'ABC Radio National',alttag:'ABC Radio National',website:'http://www.abc.net.au/rn',image:'ABCRN.gif',stream:'http://www.abc.net.au/streaming/RN.asx',locid:5,urlname:'abc_radio_national'},
{sid:62,country:'au',state:'QLD',city:'Brisbane',logname:'4RN',name:'ABC Radio National',alttag:'ABC Radio National',website:'http://www.abc.net.au/rn',image:'ABCRN.gif',stream:'http://www.abc.net.au/streaming/RN.asx',locid:4,urlname:'abc_radio_national'},
{sid:54,country:'au',state:'QLD',city:'Brisbane',logname:'4BBB',name:'B105',alttag:'B105',website:'http://www.b105.com.au/',image:'QLD_B105.gif',stream:'http://resources.b105.com.au/listenfeed/b105.asx',locid:4,urlname:'b105'},
{sid:46,country:'au',state:'SA',city:'Adelaide',logname:'5DN',name:'Cruise 1323',alttag:'Cruise 1323',website:'http://www.cruise1323.com.au/',image:'SA_CRUISE.gif',stream:'http://208.96.14.149/cruise1323',locid:5,urlname:'cruise_1323'},
{sid:74,country:'au',state:'SA',city:'Adelaide',logname:'DIGSA',name:'DIG Radio',alttag:'DIG Radio',website:'www.abc.net.au/dig',image:'DIG.gif',stream:'http://www.abc.net.au/dig/dig.asx',locid:5,urlname:'ABC_DIG_Radio'},
{sid:73,country:'au',state:'QLD',city:'Brisbane',logname:'DIGQLD',name:'DIG Radio',alttag:'DIG Radio',website:'www.abc.net.au/dig',image:'DIG.gif',stream:'http://www.abc.net.au/dig/dig.asx',locid:4,urlname:'ABC_DIG_Radio'},
{sid:69,country:'au',state:'NSW',city:'Sydney',logname:'DIGNSW',name:'DIG Radio',alttag:'DIG Radio',website:'www.abc.net.au/dig',image:'DIG.gif',stream:'http://www.abc.net.au/dig/dig.asx',locid:1,urlname:'ABC_DIG_Radio'},
{sid:72,country:'au',state:'WA',city:'Perth',logname:'DIGWA',name:'DIG Radio',alttag:'DIG Radio',website:'www.abc.net.au/dig',image:'DIG.gif',stream:'http://www.abc.net.au/dig/dig.asx',locid:3,urlname:'ABC_DIG_Radio'},
{sid:22,country:'au',state:'NSW',city:'Sydney',logname:'FBI',name:'FBI',alttag:'FBI Radio',website:'http://www.fbi.org.au',image:'NSW_FBI.gif',stream:'mms://wm9.streaming.telstra.com/UCS-wh_fbi0live1',locid:1,urlname:'FBI'},
{sid:52,country:'au',state:'SA',city:'Adelaide',logname:'5FBI',name:'Fresh FM',alttag:'Fresh FM',website:'http://www.freshfm.com.au',image:'SA_FRESHFM.gif',stream:'http://streamhq.adam.com.au:8000/FreshFM.m3u',locid:5,urlname:'fresh_fm'},
{sid:45,country:'au',state:'SA',city:'Adelaide',logname:'5ADD',name:'Mix 102.3',alttag:'Mix 102.3',website:'http://www.mix1023.com.au/',image:'SA_MIX.gif',stream:'http://208.96.14.149/mix1023',locid:5,urlname:'mix_102.3'},
{sid:29,country:'au',state:'WA',city:'Perth',logname:'6MIX',name:'MIX 94.5',alttag:'MIX 94.5',website:'http://www.mix.com.au',image:'WA_MIX.gif',stream:'http://resources.mix.com.au/listenfeed/Mix94.5livestream.asx',locid:3,urlname:'mix_94.5'},
{sid:55,country:'au',state:'QLD',city:'Brisbane',logname:'4BNE',name:'Nova 106.9',alttag:'Nova 106.9',website:'http://www.nova1069.com.au',image:'NOVA.gif',stream:'http://www.nova1069.com.au/dmgcustom/Nova1069.asx',locid:4,urlname:'nova_1066.9'},
{sid:44,country:'au',state:'SA',city:'Adelaide',logname:'5NOVA',name:'Nova 91.9',alttag:'Nova 91.9',website:'http://www.nova919.com.au',image:'NOVA.gif',stream:'http://www.nova919.com.au/dmgcustom/Nova919.asx',locid:5,urlname:'nova_91.9'},
{sid:31,country:'au',state:'WA',city:'Perth',logname:'6PER',name:'Nova 93.7',alttag:'Nova 93.7',website:'http://www.nova937.com.au',image:'NOVA.gif',stream:'http://www.nova937.com.au/dmgcustom/Nova937.asx',locid:3,urlname:'nova_93.7'},
{sid:39,country:'au',state:'WA',city:'Perth',logname:'6RED',name:'Red FM',alttag:'Red FM',website:'http://www.redfm.com.au',image:'WA_REDFM.gif',stream:'mms://audio.redfm.com.au/',locid:3,urlname:'red_fm'},
{sid:40,country:'au',state:'WA',city:'Perth',logname:'6RTR',name:'RTRfm',alttag:'RTRfm',website:'http://www.rtrfm.com.au',image:'WA_RTRFM.gif',stream:'http://www.rtrfm.com.au/live.asx',locid:3,urlname:'rtrfm'},
{sid:41,country:'au',state:'SA',city:'Adelaide',logname:'5SSA',name:'SA-FM',alttag:'SA-FM',website:'http://www.safm.com.au',image:'SA_SAFM.gif',stream:'http://resources.safm.com.au/listenfeed/safm.asx',locid:5,urlname:'sa_fm'},
{sid:23,country:'au',state:'NSW',city:'Sydney',logname:'2ONE',name:'The Edge',alttag:'The Edge',website:'http://www.961.com.au',image:'NSW_EDGE.gif',stream:'http://www.961.com.au/961.asx',locid:1,urlname:'the_edge'},
{sid:60,country:'au',state:'QLD',city:'Brisbane',logname:'4JJJ',name:'Triple J',alttag:'Triple J: New and alternative music',website:'http://www.abc.net.au/triplej',image:'JJJ.gif',stream:'http://www.abc.net.au/streaming/triplej.asx',locid:4,urlname:'triple_j'},
{sid:34,country:'au',state:'WA',city:'Perth',logname:'6JJJ',name:'Triple J',alttag:'Triple J: New and alternative music',website:'http://www.abc.net.au/triplej',image:'JJJ.gif',stream:'http://www.abc.net.au/streaming/triplej.asx',locid:3,urlname:'triple_j'},
{sid:24,country:'au',state:'NSW',city:'Sydney',logname:'2JJJ',name:'Triple J',alttag:'Triple J: New and alternative music',website:'http://www.abc.net.au/triplej',image:'JJJ.gif',stream:'http://www.abc.net.au/streaming/triplej.asx',locid:1,urlname:'triple_j'},
{sid:47,country:'au',state:'SA',city:'Adelaide',logname:'5JJJ',name:'Triple J',alttag:'Triple J: New and alternative music',website:'http://www.abc.net.au/triplej',image:'JJJ.gif',stream:'http://www.abc.net.au/streaming/triplej.asx',locid:5,urlname:'triple_j'},
{sid:53,country:'au',state:'QLD',city:'Brisbane',logname:'4MMM',name:'TRIPLE M',alttag:'TRIPLE M',website:'http://www.triplem.com.au/brisbane/index.html',image:'MMM.gif',stream:'http://resources.triplem.com.au/listenfeed/4mmmlivestreaming.asx',locid:4,urlname:'triple_m'},
{sid:42,country:'au',state:'SA',city:'Adelaide',logname:'5MMM',name:'TRIPLE M',alttag:'TRIPLE M',website:'http://www.triplem.com.au/adelaide/index.html',image:'MMM.gif',stream:'http://resources.triplem.com.au/listenfeed/5mmmstreaming.asx',locid:5,urlname:'triple_m'},
{sid:7,country:'au',state:'VIC',city:'Melbourne',logname:'3FOX',name:'101.9 THE FOX',alttag:'101.9 THE FOX Hits',website:'http://www.fox.com.au',image:'VIC_FOX.gif',stream:'http://resources.fox.com.au/listenfeed/fox.asx',locid:2,urlname:'101.9_the_fox'},
{sid:2,country:'au',state:'NSW',city:'Sydney',logname:'2MMM',name:'TRIPLE M',alttag:'TRIPLE M',website:'http://www.triplem.com.au/sydney/index.html',image:'MMM.gif',stream:'http://resources.triplem.com.au/listenfeed/2mmmlivestream.asx',locid:1,urlname:'triple_m'},
{sid:8,country:'au',state:'VIC',city:'Melbourne',logname:'3MMM',name:'TRIPLE M',alttag:'TRIPLE M: Music & Talk',website:'http://www.triplem.com.au/melbourne/index.html',image:'MMM.gif',stream:'http://resources.triplem.com.au/listenfeed/3mmmlivestream.asx',locid:2,urlname:'triple_m'},
{sid:9,country:'au',state:'VIC',city:'Melbourne',logname:'3AW',name:'3AW',alttag:'3AW: News & Talkback radio',website:'http://www.3aw.com.au',image:'VIC_3AW.gif',stream:'http://streaming.mytalk.com.au/3aw',locid:2,urlname:'3aw'},
{sid:75,country:'au',state:'QLD',city:'Brisbane',logname:'4QR',name:'612 ABC Brisbane',alttag:'612 ABC Brisbane',website:'http://www.abc.net.au/brisbane/',image:'QLD_ABC612.gif',stream:'http://www.abc.net.au/brisbane/onair/612stream.asx',locid:4,urlname:'ABC_612_Brisbane'},
{sid:4,country:'au',state:'NSW',city:'Sydney',logname:'2WFM',name:'Mix 106.5',alttag:'Mix 106.5',website:'http://www.mix1065.com.au',image:'NSW_MIX1065.gif',stream:'http://208.96.14.149/mix1065',locid:1,urlname:'mix_106.5'},
{sid:10,country:'au',state:'VIC',city:'Melbourne',logname:'MAGIC',name:'Magic 1278',alttag:'Magic 1278',website:'http://www.magic1278.com.au',image:'VIC_MAGIC.gif',stream:'mms://streaming.mytalk.com.au/magic',locid:2,urlname:'magic_1278'},
{sid:5,country:'au',state:'NSW',city:'Sydney',logname:'2SYD',name:'NOVA 96.9',alttag:'Nova 96.6',website:'http://www.nova969.com.au',image:'NSW_NOVA.gif',stream:'http://www.nova969.com.au/dmgcustom/Nova969.asx',locid:1,urlname:'nova_96.9'},
{sid:68,country:'au',state:'VIC',city:'Melbourne',logname:'3RRR',name:'Triple R FM',alttag:'Triple R FM',website:'www.rrr.org.au',image:'VIC_RRR.gif',stream:'http://media.on.net/radio/114.m3u',locid:2,urlname:'3rrr_fm'},
{sid:6,country:'au',state:'NSW',city:'Sydney',logname:'2PTV',name:'Vega 95.3',alttag:'Vega 95.3',website:'http://www.vega953.com.au',image:'NSW_VEGA.gif',stream:'http://www.vega953.com.au/dmgcustom/vega953.asx',locid:1,urlname:'vega_95.3'},
{sid:65,country:'au',state:'NSW',city:'Sydney',logname:'2GB',name:'2GB',alttag:'2GB',website:'http://www.2gb.com',image:'NSW_2GB.gif',stream:'http://podcasts.2gb.com/real-player/2GBListenLive.asx',locid:1,urlname:'2gb_sydney'},
{sid:11,country:'au',state:'VIC',city:'Melbourne',logname:'3GOLD',name:'GOLD 104.3',alttag:'GOLD 104.3: Classic Hits',website:'http://www.gold1043.com.au',image:'VIC_GOLD104.gif',stream:'http://208.96.14.149/goldfm',locid:2,urlname:'gold_104.3'},
{sid:12,country:'au',state:'VIC',city:'Melbourne',logname:'3MIX',name:'MIX 101.1',alttag:'MIX 101.1: Smooth Music',website:'http://www.mix1011.com.au',image:'VIC_MIX101.gif',stream:'http://208.96.14.149/mix1011',locid:2,urlname:'mix_101.1'},
{sid:67,country:'au',state:'VIC',city:'Melbourne',logname:'3MP',name:'3MP',alttag:'3MP Melbournes Easy Music',website:'www.3mp.com.au',image:'VIC_3MP.gif',stream:'http://3mpstream.soundaudiosolutions.com.au/3mp.asx',locid:2,urlname:'3mp'},
{sid:13,country:'au',state:'VIC',city:'Melbourne',logname:'3NOVA',name:'NOVA 100',alttag:'NOVA 100',website:'http://www.nova100.com.au',image:'VIC_NOVA.gif',stream:'http://www.nova100.com.au/dmgcustom/Nova100.asx',locid:2,urlname:'nova_100'},
{sid:14,country:'au',state:'VIC',city:'Melbourne',logname:'3VEGA',name:'VEGA 915',alttag:'VEGA 915: Music & Talk',website:'http://www.vega915.com.au',image:'VIC_VEGA.gif',stream:'http://www.vega915.com.au/dmgcustom/vega915.asx',locid:2,urlname:'vega_915'},
{sid:66,country:'au',state:'NSW',city:'Sydney',logname:'2CH',name:'2CH',alttag:'2CH',website:'www.2ch.com.au',image:'NSW_2CH.gif',stream:'http://www.2ch.com.au/listenlive/wmp_stream.php',locid:1,urlname:'2ch_sydney'},
{sid:15,country:'au',state:'VIC',city:'Melbourne',logname:'3PBS',name:'PBS 106.7 FM',alttag:'PBS 106.7 FM: Wide variety of music (Independent Community Station)',website:'http://www.pbsfm.org.au',image:'VIC_PBS.gif',stream:'http://www.pbsfm.org.au/Public/ASX/3pbsfm.asx',locid:2,urlname:'pbs_106.7_fm'},
{sid:71,country:'au',state:'VIC',city:'Melbourne',logname:'DIGVIC',name:'DIG Radio',alttag:'DIG Radio',website:'www.abc.net.au/dig',image:'DIG.gif',stream:'http://www.abc.net.au/dig/dig.asx',locid:2,urlname:'ABC_DIG_Radio'},
{sid:16,country:'au',state:'VIC',city:'Melbourne',logname:'3JJJ',name:'Triple J',alttag:'Triple J: New and alternative music',website:'http://www.abc.net.au/triplej',image:'JJJ.gif',stream:'http://www.abc.net.au/streaming/triplej.asx',locid:2,urlname:'triple_j'},
{sid:17,country:'au',state:'VIC',city:'Melbourne',logname:'3ABC774',name:'ABC 774 Melbourne',alttag:'774 ABC Melbourne: News & Talkback radio',website:'http://www.abc.net.au/melbourne',image:'VIC_ABC774.gif',stream:'http://abc.net.au/streaming/774/774stream.asx',locid:2,urlname:'abc_774_melbourne'},
{sid:18,country:'au',state:'VIC',city:'Melbourne',logname:'3ABCNR',name:'ABC News Radio',alttag:'ABC News Radio',website:'http://www.abc.net.au/newsradio',image:'ABCNR.gif',stream:'http://www.abc.net.au/streaming/newsradio.asx',locid:2,urlname:'abc_news_radio'},
{sid:19,country:'au',state:'VIC',city:'Melbourne',logname:'3ABCRN',name:'ABC Radio National',alttag:'ABC Radio National',website:'http://www.abc.net.au/rn',image:'ABCRN.gif',stream:'http://www.abc.net.au/streaming/RN.asx',locid:2,urlname:'abc_radio_national'},
{sid:20,country:'au',state:'VIC',city:'Melbourne',logname:'3BACCF',name:'ABC Classic FM',alttag:'ABC Classic FM',website:'http://www.abc.net.au/classic',image:'ABCCFM.gif',stream:'http://www.abc.net.au/streaming/classic/classicfm.asx',locid:2,urlname:'abc_classic_fm'},
{sid:21,country:'au',state:'VIC',city:'Melbourne',logname:'3SEN',name:'Sports Entertainment Network',alttag:'Sports Entertainment Network',website:'http://www.sen.com.au',image:'VIC_SEN.gif',stream:'http://sen1116.conexim.com.au/sen1116_16.asx',locid:2,urlname:'sports_entertainment_network'},
{sid:77,country:'au',state:'VIC',city:'Melbourne',logname:'',name:'Sport 927',alttag:'Sport 927',website:'www.sport927.com.au',image:'VIC_sport927.gif',stream:'http://www.sport927.com.au/Sport927_live_audio/sport927.asx',locid:2,urlname:'sport_927'}
];
排序
由于源数据无序排列,第一步需要进行排序,JavaScript的数组排序(Array.prototype.sort)支持指定排序算法。所以我编写了自己的排序算法,根据我的需求排序,我的排序逻辑是国家,州,城市,lonname的顺序进行排序,代码如下:
function JsonSort(a, b) {
if (a.country == b.country) {
if (a.state == b.state) {
if (a.city == b.city) {
return (a.logname > b.logname);
}
else {
return (a.city > b.city);
}
}
else {
return (a.state > b.state);
}
}
else {
return (a.country > b.country);
}
}
有了排序算法,在排序时只有指导排序算法就可以了。
stations.sort(JsonSort);
执行后stations就排列成我们需要的顺序,为转换做准备。
转换
转换过程就是把已经排序的数组数据整理成需要的XML格式。我需要的XML格式比较简单,就是Stations->City->Station三层。这里使用DOM来生成XML结构,DOM的介绍看 Document Object Model
var stationsNode = document.createElement("Stations");
var city = null;
for (var i=0; i < stations.length; ++i) {
if (stations[i].city != city) {
city = stations[i].city;
var cityNode = document.createElement("City");
cityNode.setAttribute("name", city);
stationsNode.appendChild(cityNode);
}
var stationNode = document.createElement("Station");
stationNode.setAttribute("sid", stations[i].sid);
stationNode.setAttribute("logname", stations[i].logname);
stationNode.setAttribute("name", stations[i].name);
stationNode.setAttribute("website", stations[i].website);
stationNode.setAttribute("image", stations[i].image);
stationNode.setAttribute("stream", stations[i].stream);
cityNode.appendChild(stationNode);
}
//var xml = CreateXmlDoc("", "XML");
var xml = document.createElement("XML");
xml.appendChild(stationsNode);
document.createElement()函数生成节点,setAttribute()函数设置节点的属性,appendChild()还是把节点添加到父节点中。
显示
其实上面是转换过程已经完成了XML生成的全部操作,可是XML不能直接显示在HTML中,所以需要对生成的XML进行修改来正常显示。
var str = xml.innerHTML;
var str2 = "";
var level = 0;
for (i = 0; i < str.length; ++i) {
if (str[i] == "<") {
if (str[i + 1] == "/") {
--level;
for (j = 0; j < level; ++j) {
str2 += " ";
}
}
else {
for (j = 0; j < level; ++j) {
str2 += " ";
}
++level;
}
str2 += "<";
}
else if (str[i] == ">") {
str2 += "><br />";
}
else {
str2 += str[i];
}
}
把大于号和小于号进行替换,同时根据层次显示缩进。
结果
把结果显示到HTML中。
<html>
<head>
<title>Json to Xml</title>
</head>
<body>
<pre title="Result" id="Result">
</pre>
</body>
</html>
var result = document.getElementById("Result");
result.innerHTML = str2;
Code
<stations>
<city name="Sydney">
<station stream="http://www.abc.net.au/streaming/newsradio.asx" image="ABCNR.gif" website="http://www.abc.net.au/newsradio" name="ABC News Radio" logname="2ABCNR" sid="26">
</station>
<station stream="http://www.abc.net.au/streaming/RN.asx" image="ABCRN.gif" website="http://www.abc.net.au/rn" name="ABC Radio National" logname="2ABCRN" sid="27">
</station>
<station stream="http://www.abc.net.au/streaming/classic/classicfm.asx" image="ABCCFM.gif" website="http://www.abc.net.au/classic" name="ABC Classic FM" logname="2BACCF" sid="28">
</station>
<station stream="http://abc.net.au/streaming/702/702stream.asx" image="NSW_ABC702.gif" website="http://www.abc.net.au/sydney" name="ABC 702 Sydney" logname="2BL" sid="25">
</station>
<station stream="http://www.2ch.com.au/listenlive/wmp_stream.php" image="NSW_2CH.gif" website="www.2ch.com.au" name="2CH" logname="2CH" sid="66">
</station>
<station stream="http://resources.2dayfm.com.au/listenfeed/2dayfm.asx" image="NSW_2DAY.gif" website="http://www.2dayfm.com.au" name="2 Day FM" logname="2DAY" sid="1">
</station>
<station stream="http://podcasts.2gb.com/real-player/2GBListenLive.asx" image="NSW_2GB.gif" website="http://www.2gb.com" name="2GB" logname="2GB" sid="65">
</station>
<station stream="http://www.abc.net.au/streaming/triplej.asx" image="JJJ.gif" website="http://www.abc.net.au/triplej" name="Triple J" logname="2JJJ" sid="24">
</station>
<station stream="http://resources.triplem.com.au/listenfeed/2mmmlivestream.asx" image="MMM.gif" website="http://www.triplem.com.au/sydney/index.html" name="TRIPLE M" logname="2MMM" sid="2">
</station>
<station stream="http://www.961.com.au/961.asx" image="NSW_EDGE.gif" website="http://www.961.com.au" name="The Edge" logname="2ONE" sid="23">
</station>
<station stream="http://www.vega953.com.au/dmgcustom/vega953.asx" image="NSW_VEGA.gif" website="http://www.vega953.com.au" name="Vega 95.3" logname="2PTV" sid="6">
</station>
<station stream="http://www.nova969.com.au/dmgcustom/Nova969.asx" image="NSW_NOVA.gif" website="http://www.nova969.com.au" name="NOVA 96.9" logname="2SYD" sid="5">
</station>
<station stream="http://streaming.mytalk.com.au/2ue" image="NSW_2UE.gif" website="http://www.2ue.com.au" name="2UE Talkback Radio" logname="2UE" sid="3">
</station>
<station stream="http://208.96.14.149/mix1065" image="NSW_MIX1065.gif" website="http://www.mix1065.com.au" name="Mix 106.5" logname="2WFM" sid="4">
</station>
<station stream="http://www.abc.net.au/dig/dig.asx" image="DIG.gif" website="www.abc.net.au/dig" name="DIG Radio" logname="DIGNSW" sid="69">
</station>
<station stream="mms://wm9.streaming.telstra.com/UCS-wh_fbi0live1" image="NSW_FBI.gif" website="http://www.fbi.org.au" name="FBI" logname="FBI" sid="22">
</station>
</city>
<city name="Brisbane">
<station stream="http://www.abc.net.au/streaming/classic/classicfm.asx" image="ABCCFM.gif" website="http://www.abc.net.au/classic" name="ABC Classic FM" logname="4BACCF" sid="63">
</station>
<station stream="http://resources.b105.com.au/listenfeed/b105.asx" image="QLD_B105.gif" website="http://www.b105.com.au/" name="B105" logname="4BBB" sid="54">
</station>
<station stream="http://streaming.mytalk.com.au/4bc" image="QLD_4BC.gif" website="http://www.4bc.com.au" name="4BC" logname="4BC" sid="58">
</station>
<station stream="http://208.96.14.149/973fm" image="QLD_973FM.gif" website="http://www.973fm.com.au/" name="97.3 FM" logname="4BFM" sid="56">
</station>
<station stream="http://streaming.mytalk.com.au/4bh" image="QLD_4BH.gif" website="http://www.4bh.com.au/" name="4BH" logname="4BH" sid="59">
</station>
<station stream="http://www.nova1069.com.au/dmgcustom/Nova1069.asx" image="NOVA.gif" website="http://www.nova1069.com.au" name="Nova 106.9" logname="4BNE" sid="55">
</station>
<station stream="http://www.abc.net.au/streaming/triplej.asx" image="JJJ.gif" website="http://www.abc.net.au/triplej" name="Triple J" logname="4JJJ" sid="60">
</station>
<station stream="http://www.4kq.com.au/media/4kq/streaming/4kq.asx" image="QLD_4KQ.gif" website="http://www.4kq.com.au/" name="4KQ" logname="4KQ" sid="57">
</station>
<station stream="http://resources.triplem.com.au/listenfeed/4mmmlivestreaming.asx" image="MMM.gif" website="http://www.triplem.com.au/brisbane/index.html" name="TRIPLE M" logname="4MMM" sid="53">
</station>
<station stream="http://www.abc.net.au/streaming/newsradio.asx" image="ABCNR.gif" website="http://www.abc.net.au/newsradio" name="ABC News Radio" logname="4PB" sid="61">
</station>
<station stream="http://www.abc.net.au/brisbane/onair/612stream.asx" image="QLD_ABC612.gif" website="http://www.abc.net.au/brisbane/" name="612 ABC Brisbane" logname="4QR" sid="75">
</station>
<station stream="http://www.abc.net.au/streaming/RN.asx" image="ABCRN.gif" website="http://www.abc.net.au/rn" name="ABC Radio National" logname="4RN" sid="62">
</station>
<station stream="http://www.abc.net.au/dig/dig.asx" image="DIG.gif" website="www.abc.net.au/dig" name="DIG Radio" logname="DIGQLD" sid="73">
</station>
</city>
<city name="Adelaide">
<station stream="http://www.fiveaa.com.au/dmgcustom/fiveaa.asx" image="SA_5AA.gif" website="http://www.5aa.com.au" name="5AA" logname="5AA" sid="43">
</station>
<station stream="http://208.96.14.149/mix1023" image="SA_MIX.gif" website="http://www.mix1023.com.au/" name="Mix 102.3" logname="5ADD" sid="45">
</station>
<station stream="http://www.abc.net.au/streaming/classic/classicfm.asx" image="ABCCFM.gif" website="http://www.abc.net.au/classic" name="ABC Classic FM" logname="5BACCF" sid="51">
</station>
<station stream="http://208.96.14.149/cruise1323" image="SA_CRUISE.gif" website="http://www.cruise1323.com.au/" name="Cruise 1323" logname="5DN" sid="46">
</station>
<station stream="http://streamhq.adam.com.au:8000/FreshFM.m3u" image="SA_FRESHFM.gif" website="http://www.freshfm.com.au" name="Fresh FM" logname="5FBI" sid="52">
</station>
<station stream="http://www.abc.net.au/streaming/triplej.asx" image="JJJ.gif" website="http://www.abc.net.au/triplej" name="Triple J" logname="5JJJ" sid="47">
</station>
<station stream="http://resources.triplem.com.au/listenfeed/5mmmstreaming.asx" image="MMM.gif" website="http://www.triplem.com.au/adelaide/index.html" name="TRIPLE M" logname="5MMM" sid="42">
</station>
<station stream="http://www.nova919.com.au/dmgcustom/Nova919.asx" image="NOVA.gif" website="http://www.nova919.com.au" name="Nova 91.9" logname="5NOVA" sid="44">
</station>
<station stream="http://www.abc.net.au/streaming/newsradio.asx" image="ABCNR.gif" website="http://www.abc.net.au/newsradio" name="ABC News Radio" logname="5PB" sid="49">
</station>
<station stream="http://www.abc.net.au/streaming/RN.asx" image="ABCRN.gif" website="http://www.abc.net.au/rn" name="ABC Radio National" logname="5RN" sid="50">
</station>
<station stream="http://resources.safm.com.au/listenfeed/safm.asx" image="SA_SAFM.gif" website="http://www.safm.com.au" name="SA-FM" logname="5SSA" sid="41">
</station>
<station stream="http://www.abc.net.au/adelaide/onair/891stream.asx" image="SA_ABC720.gif" website="http://www.abc.net.au/adelaide/" name="ABC 891 Adelaide" logname="5WF" sid="48">
</station>
<station stream="http://www.abc.net.au/dig/dig.asx" image="DIG.gif" website="www.abc.net.au/dig" name="DIG Radio" logname="DIGSA" sid="74">
</station>
</city>
<city name="Melbourne">
<station stream="http://www.sport927.com.au/Sport927_live_audio/sport927.asx" image="VIC_sport927.gif" website="www.sport927.com.au" name="Sport 927" logname="" sid="77">
</station>
<station stream="http://abc.net.au/streaming/774/774stream.asx" image="VIC_ABC774.gif" website="http://www.abc.net.au/melbourne" name="ABC 774 Melbourne" logname="3ABC774" sid="17">
</station>
<station stream="http://www.abc.net.au/streaming/newsradio.asx" image="ABCNR.gif" website="http://www.abc.net.au/newsradio" name="ABC News Radio" logname="3ABCNR" sid="18">
</station>
<station stream="http://www.abc.net.au/streaming/RN.asx" image="ABCRN.gif" website="http://www.abc.net.au/rn" name="ABC Radio National" logname="3ABCRN" sid="19">
</station>
<station stream="http://streaming.mytalk.com.au/3aw" image="VIC_3AW.gif" website="http://www.3aw.com.au" name="3AW" logname="3AW" sid="9">
</station>
<station stream="http://www.abc.net.au/streaming/classic/classicfm.asx" image="ABCCFM.gif" website="http://www.abc.net.au/classic" name="ABC Classic FM" logname="3BACCF" sid="20">
</station>
<station stream="http://resources.fox.com.au/listenfeed/fox.asx" image="VIC_FOX.gif" website="http://www.fox.com.au" name="101.9 THE FOX" logname="3FOX" sid="7">
</station>
<station stream="http://208.96.14.149/goldfm" image="VIC_GOLD104.gif" website="http://www.gold1043.com.au" name="GOLD 104.3" logname="3GOLD" sid="11">
</station>
<station stream="http://www.abc.net.au/streaming/triplej.asx" image="JJJ.gif" website="http://www.abc.net.au/triplej" name="Triple J" logname="3JJJ" sid="16">
</station>
<station stream="http://208.96.14.149/mix1011" image="VIC_MIX101.gif" website="http://www.mix1011.com.au" name="MIX 101.1" logname="3MIX" sid="12">
</station>
<station stream="http://resources.triplem.com.au/listenfeed/3mmmlivestream.asx" image="MMM.gif" website="http://www.triplem.com.au/melbourne/index.html" name="TRIPLE M" logname="3MMM" sid="8">
</station>
<station stream="http://3mpstream.soundaudiosolutions.com.au/3mp.asx" image="VIC_3MP.gif" website="www.3mp.com.au" name="3MP" logname="3MP" sid="67">
</station>
<station stream="http://www.nova100.com.au/dmgcustom/Nova100.asx" image="VIC_NOVA.gif" website="http://www.nova100.com.au" name="NOVA 100" logname="3NOVA" sid="13">
</station>
<station stream="http://www.pbsfm.org.au/Public/ASX/3pbsfm.asx" image="VIC_PBS.gif" website="http://www.pbsfm.org.au" name="PBS 106.7 FM" logname="3PBS" sid="15">
</station>
<station stream="http://media.on.net/radio/114.m3u" image="VIC_RRR.gif" website="www.rrr.org.au" name="Triple R FM" logname="3RRR" sid="68">
</station>
<station stream="http://sen1116.conexim.com.au/sen1116_16.asx" image="VIC_SEN.gif" website="http://www.sen.com.au" name="Sports Entertainment Network" logname="3SEN" sid="21">
</station>
<station stream="http://www.vega915.com.au/dmgcustom/vega915.asx" image="VIC_VEGA.gif" website="http://www.vega915.com.au" name="VEGA 915" logname="3VEGA" sid="14">
</station>
<station stream="http://www.abc.net.au/dig/dig.asx" image="DIG.gif" website="www.abc.net.au/dig" name="DIG Radio" logname="DIGVIC" sid="71">
</station>
<station stream="mms://streaming.mytalk.com.au/magic" image="VIC_MAGIC.gif" website="http://www.magic1278.com.au" name="Magic 1278" logname="MAGIC" sid="10">
</station>
</city>
<city name="Perth">
<station stream="http://www.abc.net.au/streaming/classic/classicfm.asx" image="ABCCFM.gif" website="http://www.abc.net.au/classic" name="ABC Classic FM" logname="6BACCF" sid="38">
</station>
<station stream="http://www.abc.net.au/streaming/triplej.asx" image="JJJ.gif" website="http://www.abc.net.au/triplej" name="Triple J" logname="6JJJ" sid="34">
</station>
<station stream="http://resources.mix.com.au/listenfeed/Mix94.5livestream.asx" image="WA_MIX.gif" website="http://www.mix.com.au" name="MIX 94.5" logname="6MIX" sid="29">
</station>
<station stream="http://streaming.amnet.net.au/96fm" image="WA_96FM.gif" website="http://www.96fm.com.au" name="96FM" logname="6NOW" sid="32">
</station>
<station stream="http://www.abc.net.au/streaming/newsradio.asx" image="ABCNR.gif" website="http://www.abc.net.au/newsradio" name="ABC News Radio" logname="6PB" sid="36">
</station>
<station stream="http://www.nova937.com.au/dmgcustom/Nova937.asx" image="NOVA.gif" website="http://www.nova937.com.au" name="Nova 93.7" logname="6PER" sid="31">
</station>
<station stream="http://resources.929.com.au/listenfeed/929.asx" image="WA_929.gif" website="http://www.929.com.au" name="92.9" logname="6PPM" sid="30">
</station>
<station stream="http://streaming.mytalk.com.au/6pr" image="WA_6PR.gif" website="http://www.6pr.com.au" name="882 6PR" logname="6PR" sid="33">
</station>
<station stream="mms://audio.redfm.com.au/" image="WA_REDFM.gif" website="http://www.redfm.com.au" name="Red FM" logname="6RED" sid="39">
</station>
<station stream="http://www.abc.net.au/streaming/RN.asx" image="ABCRN.gif" website="http://www.abc.net.au/rn" name="ABC Radio National" logname="6RN" sid="37">
</station>
<station stream="http://www.rtrfm.com.au/live.asx" image="WA_RTRFM.gif" website="http://www.rtrfm.com.au" name="RTRfm" logname="6RTR" sid="40">
</station>
<station stream="http://abc.net.au/perth/onair/720stream.asx" image="WA_ABC720.gif" website="http://www.abc.net.au/perth" name="ABC 720 Perth" logname="6WF" sid="35">
</station>
<station stream="http://www.abc.net.au/dig/dig.asx" image="DIG.gif" website="www.abc.net.au/dig" name="DIG Radio" logname="DIGWA" sid="72">
</station>
</city>
</stations>
总结
进行Json到XML转换的开发,需要先定义出转换后的XML的格式,然后根据该格式对Json数据进行排序,把排序后的数据转换到DOM的XML结构里,最后转换显示格式进行显示。
下一篇回到C++, tinyXML。
源码:JsonToXml.htm
环境: Firefox 3.5 + FireBug 1.4
关于Mobile Radio - Internet Radio Software for Windows Mobile项目
目前(2009年9月份)这个项目基本功能已经完成,只是界面方面需要改进,提高用户体验。我把项目host到
Mobile Radio - Internet Radio Software for Windows Mobile了,我会持续改进,主要是提高用户体验方面。
需要了解项目最新动态,可以访问
Mobile Radio - Internet Radio Software for Windows Mobile 和我的Blog
精简开发 无线生活。
源代码: 查看Mobile Radio最新源代码
环境:VS2008 + WM 6 professional SDK + WTL 8.1 + TinyXML