Archive for 七月, 2008

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
Powered by WordPress | Shop Free Cellular Phones at Bestincellphones.com. | Thanks to Best CD Rates, iCellPhoneDeals.com Offers Best Cell Phone Deals. and Incinerador De Grasa