小鹅

喜欢DOTA,关注JavaScript、jQuery、ActionScript、CodeIgniter、PHP、MySQL等相关技术...
存档 2008年07月18日 的所有文章

Javascript和PHP的xml to json

最近在写一个yupoo应用,基于ajax的,所以需要将xml转换为json,在网上搜索后找到两个不错的类
PHP服务端的XMLtoJSON

<?php
require_once("xml2json.php");
echo Xml2json::transformXmlStringToJson(xml_data);
?>

Javascript客户端的XMLtoJSON

<!-- HTML -->
<head>
<script type="text/javascript" src="xml2json.js"></script>
...
</head>
// Javascript
// A simple call - myXML is a string containing your XML:
myJsonObject=xml2json.parser(myXML);

// A 2:nd, optional, parameter is "tags not to convert" - for example <b> and <i>:
myJsonObject=xml2json.parser(myXML,'b,i');

// A 3:rd, optional, parameter gives us a string showing us the JSON structure
// instead of the actual JSON object:
myString=xml2json.parser(myXML,'','html');

// - use "compact" for output without linebreaks or tabbing
// - use "normal" for output with linebreaks and tabbing
// - use "html" for a html representation
18 2008