XML干扰javascript变量

时间:2014-09-02 04:08:24

标签: javascript html xml svg

这是我正在进行的项目的简化版本。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">
    var bridge;

    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","MarketSpreadsheet.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 


    bridge = "xml data goes in here";

</script>


<svg height="300" width="300">
    <text id="thePopUp" x="150" y="150" font-size="30" fill="black" visibility="visible"></text>
</svg>

<script type="text/javascript">
thePopUp.textContent = bridge;
</script>

</body>
</html>

我正在尝试创建一个动态svg元素,由于某种原因,将xml字符串直接解析为thePopUp.textContent无法正常工作。我的计划是将xml数据转换为javascript变量矩阵,并使用它来分配文本内容。我的问题是xml.http.send(); line使我的变量赋值无效,使输出“未定义”。我需要该行来访问我的xml文件,但该行阻止我将xml分配给变量(或者任何变量到该变量)。怎么回事?

1 个答案:

答案 0 :(得分:0)

SAMPLE XML 
<exam courseName="MS Word" methodTxt="WRITE THE CORRECT OPTION IN ANSWER FIELD" exam_no="1" blankFeedback="Please write your answer">
    <item>
        <question><![CDATA[What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?tapash]]></question>
        <acceptable_option this_question_marks="2">
            <option marks="2"><![CDATA[Tabs]]></option>
            <option marks=".4"><![CDATA[Buttons]]></option>
            <option marks=".4"><![CDATA[Check boxes]]></option>
            <option marks=".4"><![CDATA[Dialog boxes]]></option>
        </acceptable_option>
    </item>
    <item>
        <question><![CDATA[The Ribbon is divided into]]></question>
        <acceptable_option this_question_marks="2">
            <option marks="2"><![CDATA[Tabs]]></option>
        </acceptable_option>
    </item>
</exam>

AND JAVASCRIPT IS 
if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","xml/fill_in_the_blank.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;


    myXMLDoc=xmlDoc.getElementsByTagName('item');
    totalQuestion = myXMLDoc.length;

$( “#thePopUp”)。文本(totalQuestion)

但是你需要使用“JQUERY”和“网络服务器或像手术一样”