我正在关注https://developer.mozilla.org/en-US/docs/JXON 编写一个将 xml 文件转换为 JSON 的算法。但是,没有运气。我觉得我做错了什么!
我应该在以下行中通过什么来代替“doc”:
var myObject = new JXONTree(doc);
我试过这个:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
<script type="text/javascript" src="sample1.js"></script>
<script type="text/javascript">
/*
* Loading xml on the browser page
*/
function loadXML()
{
try
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "sample.xml", false);
xmlhttp.send();
}
catch(e)
{
alert("Please enable file access to read config file");
}
var response = xmlhttp.responseXML;
if (response == null)
{
alert("Error in xml file ..Please check config.xml file is valid or not !");
}
return response;
}
function myFun()
{
//Loading config xml
var xmlDoc = loadXML();
var myObject = new JXONTree(xmlDoc);
alert(myObject);
}
</script>
</head>
<body onload="myFun()">
Hello
</body>
</html>
我的 sample.xml :
<?xml version="1.0"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<discount_until>Dec 25, 1995</discount_until>
<price>42.50</price>
<size description="Medium">
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
<script type="text/javascript"><![CDATA[function matchwo(a,b) {
if (a < b && a < 0) { return 1; }
else { return 0; }
}]]></script>
</catalog>
因此,在运行此程序时,我在 Opera 中收到如下错误:
请启用文件访问以读取配置文件。我在 Opera12 中启用了 Allow File XMLHttpRequest。还是没有运气!它适用于谷歌浏览器。那么我错过了什么?
请帮忙。
谢谢