2012-01-23 19 views
0

こんにちは、xmlをツリー構造に変換したいと思います。しかし、私はローカルマシンからのパスを与えるときはうまく動作しますが、私たちがURLパスを与えないと動作しません。javascriptを使用してxmlをツリー構造に変換する

どうすればいいですか?

私は次のコードを使用しています。

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: "" 
    }); 
} 

どうすればいいですか? 。

+0

エラーは何ですか? –

+0

私はdivでこれにアクセスしていますが、divには何も入っていません。 –

+0

xmlパスをよく見てみましょう。多分同じフォルダにないのでしょうか? –

答えて

0

ウェブページをローカルで開く場合、絶対リソースへのアクセスはhttp://de.wikipedia.org/wiki/Same-Origin-Policyのため失敗します。 同じドメインとポートからページを開く必要があります。

関連する問題