[問題] XML to Json 如何取得顯示..?
呼.. 搞了好幾天的 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
推
08/08 22:03, , 1F
08/08 22:03, 1F
→
08/08 22:04, , 2F
08/08 22:04, 2F
→
08/08 22:04, , 3F
08/08 22:04, 3F
→
08/08 22:05, , 4F
08/08 22:05, 4F
→
08/09 00:57, , 5F
08/09 00:57, 5F
→
08/09 01:19, , 6F
08/09 01:19, 6F
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章