Ajax 板


LINE

呼.. 搞了好几天的 XML,迟迟无法让我正常使用。 於是想改用 json试试。 但看了 JQuery 的范例,实在不解该怎麽使用.... 以下是我的程式,这是转换成json的型态。 有大大可教导我该如何用JQuery去呈现和使用吗...? ------------------------------------------------ xmltojson.php ------------------------------------------------ <?php $rss = file_get_contents('http://tw.tranews.com/map/dStores.asp?x1=120680610&x2=120687040&y1=24148140&y2=24152240'); $json = Xml2Json($rss); echo $json; function Xml2Json($xml_data) { $xml2Array = new xml2Array(); $xml = $xml2Array->parse($xml_data); $json = array(); for($i = 0, $c = count($xml); $i < $c; $i++) { array_push($json, Xml2JsonSub($xml[$i])); } return '{'. implode(', ', $json) .'}'; } function Xml2JsonKey($xml) { return '"' . $xml['NAME'] . '"'; } function Xml2JsonValue($xml) { $values = array(); if (isset($xml['ATTR']) && is_array($xml['ATTR']) && count($xml['ATTR'])) { foreach ($xml['ATTR'] as $k => $v) { $values["@$k"] = '"' . $v . '"'; } } if (isset($xml['DATA'])) { $values['#text'] = '"' . $xml['DATA'] . '"'; } if (isset($xml['SUB']) && is_array($xml['SUB']) && count($xml['SUB'])) { foreach ($xml['SUB'] as $name => $sub) { $_sub = array(); if (isset($sub[0]['NAME'])) { $subarray = array(); foreach ($sub as $s) { array_push($subarray, Xml2JsonValue($s)); } $values[$name] = '[ ' . implode(', ', $subarray) . ' ]'; } else { $values[$name] = Xml2JsonValue($sub); } } } if (!count($values)) return 'null'; elseif (count($values) == 1 && isset($values['#text'])) return $values['#text']; else { $ret = array(); foreach ($values as $k => $v) { array_push($ret, '"' . $k . '": ' . $v); } return '{ ' . implode(', ', $ret) . ' }'; } } function Xml2JsonSub($xml) { return Xml2JsonKey($xml) . ': ' . Xml2JsonValue($xml); } class xml2Array { var $out = array(); var $parser; var $data; function parse($strInputXML) { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, "tagOpen", "tagClosed"); xml_set_character_data_handler($this->parser, "tagData"); $this->data = xml_parse($this->parser, $strInputXML); if (!$this->data) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } xml_parser_free($this->parser); return $this->out; } function tagOpen($parser, $name, $attrs) { $tag = array(); $tag['NAME'] = strtolower($name); if (count($attrs)) { $tag['ATTR'] = array(); foreach ($attrs as $k => $v) $tag['ATTR'][strtolower($k)] = $v; } array_push($this->out, $tag); } function tagData($parser, $tagData) { $tagData = addslashes(trim($tagData)); if (strlen($tagData)) { if(isset($this->out[count($this->out)-1]['DATA'])) { $this->out[count($this->out)-1]['DATA'] .= $tagData; } else { $this->out[count($this->out)-1]['DATA'] = $tagData; } } } function tagClosed($parser, $name) { $child = $this->out[count($this->out)-1]; $name = $child['NAME']; if (isset($this->out[count($this->out)-2]['SUB'][$name][0]['NAME'])) { $this->out[count($this->out)-2]['SUB'][$name][] = $child; } elseif (isset($this->out[count($this->out)-2]['SUB'][$name]['NAME'])) { $prev = $this->out[count($this->out)-2]['SUB'][$name]; $this->out[count($this->out)-2]['SUB'][$name] = array($prev, $child); } else { $this->out[count($this->out)-2]['SUB'][$name] = $child; } array_pop($this->out); } } ?> --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 163.17.131.249
1F:推 s25g5d4:我个人是认为...json xml互转可能会依各文件而有不同... 08/08 22:03
2F:→ s25g5d4:再者原文件制作者的习惯你也不可能摸透... 08/08 22:04
3F:→ s25g5d4:所以不好写出通用的转换器... 08/08 22:04
4F:→ s25g5d4:可以的话 吃xml就喂xml 吃json就喂json... 08/08 22:05
5F:→ TonyQ:你要不要先看一下yahoo pipe 有没有能直接整得? 08/09 00:57
6F:→ oao54bb:That's too bad. 因为我们就是遇到XML无法喂XML.. 08/09 01:19







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP