使用javascript将xml转换为树结构
convert xml into tree structure using javascript
嗨,我想把xml转换成树结构。但是当我从本地机器给出路径时,它工作得很好,但当我们给出url路径时,却不工作。
我怎么能那样做。
我正在使用以下代码。
var strList="";
var level=0;
var curriculumXml ="";
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "xml/development_curriculum.xml(i have to place the weburl here.like http://www.cde.com/development_curriculum.xml",
dataType: "xml",
success: function(data)
{
manipulateXml(data);
$("#curTree").html();
loadParentCur();
}
});
});
/*This method is to get search curriculum XML list*/
function manipulateXml(data)
{
curriculumXml = data;
$(data).find("node").each(function(){
var id = parseInt($(this).attr("id"));
});
}
/*This method is used to get curriculum tree*/
function loadParentCur()
{
if (window.ActiveXObject)
{
var xmlobject=new ActiveXObject("Microsoft.XMLDOM");
xmlobject.async="false";
var xmlString = curriculumXml.xml;
}
// code for Mozilla, Firefox, Opera, etc.
else
{
var xmlString = (new XMLSerializer()).serializeToString(curriculumXml);
}
var json = $.xml2json(xmlString);
var objNode = json.node;
renderTree(objNode);
}
function to_ul(branches)
{
if(branches !=undefined)
{
if(level>0)
{
strList = strList + "<ul style='"display: none;'">";
}
if(branches.length == undefined)
{
var branch = branches.node;
if (branch)
{
strList = strList + "<li class='"expandable'" ><div class='"hitarea expandable-hitarea'" onclick='"#'"></div><span><a href='"#'" onclick='"#'">";
strList = strList + branches.name+"</a></span>";
level=level+1;
to_ul(branch);
}
else
{
strList = strList + "<li id='""+branches.id+"'" onclick='"#'" style='"white-space: nowrap;list-style: none;margin-left:-25px;'"><a href='"#'" class='"but_res'" style='"width:152px; margin-left:30px;'">";
strList = strList + unescape(branches.name)+"</a>";
}
strList = strList +"</li>";
level=0;
}
else
{
for (var i=0; i<branches.length; i++)
{
var branch = branches[i];
if (branch.node)
{
strList = strList + "<li class='"expandable'" ><div class='"hitarea expandable-hitarea'" onclick='"#'"></div><span><a href='"#'" onclick='"#'">";
strList = strList + branch.name+"</a></span>";
level=level+1;
to_ul(branch.node);
}
else
{
strList = strList + "<li id='""+branch.id+"'" onclick='"#'" style='"white-space: nowrap;list-style: none;margin-left:-25px;'"><a href='"#'" class='"but_res'" style='"width:152px; margin-left:30px;'">";
strList = strList + unescape(branch.name)+"</a>";
}
strList = strList +"</li>";
level=0;
}
}
strList =strList + "</ul>";
}
}
/*This method is used to render tree*/
function renderTree(objTree)
{
var objTreeNew = objTree;
to_ul(objTreeNew);
$("#curTree").html("<ul class='"treeview'" id='"tree'">"+strList+"</ul>");
$("#tree").treeview({
collapsed: true,
animated: "medium",
control:"#sidetreecontrol",
prerendered: true,
persist: ""
});
}
我们怎么能做到这一点。
如果您在本地打开网页,访问绝对资源将失败,原因是http://de.wikipedia.org/wiki/Same-Origin-Policy.您必须从同一域和端口打开页面。
相关文章:
- 如何将我的json结构转换为C3.js所需的列结构
- 将数据结构转换为二进制数据
- 将json结构转换为可用的javascript数据集
- 无法识别的数据结构-转换为对象
- 与restify一起,我的帖子显示了一个“;将循环结构转换为JSON”;错误
- io.sockets.clients()给出错误“”;将循环结构转换为JSON”;
- 如何将重叠结构转换为 js-ctypes
- 使用 Node.js 将文件系统中的目录结构转换为 JSON
- JQuery TypeError:在MVC中将循环结构转换为JSON
- Uncatch TypeError:在 Jquery.each 中使用 JSON.stringfy 将循环结构转换为 J
- 如何序列化全日历源.将循环结构转换为 JSON 错误
- 类型错误:将循环结构转换为 JSON:有没有办法忽略此警告
- 注入带有流星会话返回的脚本,将循环结构转换为JSON
- 使用Django模板(列表和dicts)将Python数据结构转换为js数据结构
- 如何操作对象's结构转换成'点符号'-样式数组
- img ng-src:TypeError:将循环结构转换为JSON
- 使用javascript将Sharepoint在线文件结构转换为json文件
- 将嵌套数据结构转换为使用类对象
- 如何将此数据结构转换为数组
- 如何将JSON对象结构转换为点表示法