2013-03-20 64 views
1

我正在開發一個應用程序,點擊按鈕時,程序調用存儲在XML文件中的信息,以使用JavaScript在<span>標記中顯示;解析XML文件不起作用

function viewXMLFiles() { 

     console.log("viewXMLFiles() is running"); 

     var xmlhttp = new HttpRequest(); 
      xmlhttp.open("GET", "TestInfo.xml", false); 
      xmlhttp.send; 

     console.log("still running"); 

     var xmlDoc = xmlhttp.responseXML; 

     console.log("getting tired"); 

     document.getElementById("documentList").innerHTML = xmlDoc.getElementByTagName("documentList")[0].childNodes[0].nodeValue; 
     document.getElementById("documentList").innerHTML = xmlDoc.getElementByTagName("documentList")[1].childNodes[1].nodeValue; 

     console.log("done"); 
    } 

然後調用它的HTML是(以及將顯示XML文件的位置);

<button onclick = "viewXMLFiles();">View Document Info</button><br> 

<span id = "documentList"> 
    <!--This is where the XML will be loaded into--> 
</span> 

該XML文件是;

<document_list> 

<document> 

    <document_name>Holidays.pdf</document_name> 

    <file_type>.pdf</file_type> 

    <file_location></file_location> 

</document> 

<document> 

    <document_name>iPhone.jsNotes.docx</document_name> 

    <file_type>.docx</file_type> 

    <file_location></file_location> 

</document> 

</document_list> 

在控制檯中,出現第一條消息,但沒有任何反應,這就是所有的結果。但我真的(如,真的新)到XML和解析,並不明白是什麼錯。你能幫忙嗎?

+1

您是否嘗試過使用XMLHttpRequest? – Marek 2013-03-20 11:58:15

+0

我說'新的HttpRequest'? – 2013-03-20 11:59:17

+0

好吧我試過了,現在它打破了'document.getElementById'行 – 2013-03-20 12:00:39

回答

0

如果你想顯示每個文檔的信息,你會想這樣的事情:

<html> 
<head> 

<script type="text/javascript"> 
function getMyXML() { 

    console.log("viewXMLFiles() is running"); 

    xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open("GET","TestInfo.xml",false); 
    xmlhttp.send(); 
    xmlDoc = xmlhttp.responseXML; 

    console.log("still running"); 

    console.log("getting tired"); 

    document.getElementById("docname").innerHTML = xmlDoc.getElementsByTagName("document_name")[0].childNodes[0].nodeValue; 
    document.getElementById("filetype").innerHTML = xmlDoc.getElementsByTagName("file_type")[0].childNodes[0].nodeValue; 

    document.getElementById("docname2").innerHTML = xmlDoc.getElementsByTagName("document_name")[1].childNodes[0].nodeValue; 
    document.getElementById("filetype2").innerHTML = xmlDoc.getElementsByTagName("file_type")[1].childNodes[0].nodeValue; 

    console.log("done"); 
} 
</script> 
</head> 
<body> 

<input type="button" onclick="getMyXML();" value="Get XML" /> 

<div id="doclist"> 
<h2>Document 1</h2> 
<label>Docname: </label><span id="docname"></span><br/> 
<label>Filetype: </label><span id="filetype"></span><br/> 

</div> 

<div id="doclist"> 
<h2>Document 2</h2> 
<label>Docname: </label><span id="docname2"></span><br/> 
<label>Filetype: </label><span id="filetype2"></span><br/> 
</div> 

</body> 
1

我認爲這個問題是:

var xmlhttp = new HttpRequest(); 

它應該是:

var xmlhttp = new XMLHttpRequest(); 
+0

好,非常感謝 - 這很有效,但現在JavaScript的最後兩行有問題:S你能看到一個問題嗎? – 2013-03-20 12:02:36

2

使用這都是因爲你的documentlist

document.getElementById("documentList").innerHTML = xmlDoc.getElementByTagName("documentList")[0].childNodes[0].nodeValue; 
document.getElementById("documentList").innerHTML = xmlDoc.getElementByTagName("documentList")[1].childNodes[0].nodeValue; 
+0

我試過了,它沒有改變任何東西 – 2013-03-20 12:05:36

+0

你得到了什麼......?它只讀Holidays.pdf .. ?? – 2013-03-20 12:06:50

+0

我在聲明'xmlDoc'時做了錯誤嗎? – 2013-03-20 12:07:29

2

注只有一個childnode:

  1. 使用的getElementsByTagName
  2. 有一個在你的XML沒有documentList標籤
  3. 標籤文件是在XML中唯一陣列不document_list

    function viewXMLFiles() { 
    
        console.log("viewXMLFiles() is running"); 
    
        xmlhttp = new XMLHttpRequest(); 
        xmlhttp.open("GET","TestInfo.xml",false); 
        xmlhttp.send(); 
        xmlDoc = xmlhttp.responseXML; 
    
        console.log("still running"); 
    
    
        var getData = xmlDoc.getElementsByTagName("document"); 
        console.log("getting tired"); 
    
        document.getElementById("documentList").innerHTML = getData[0].getElementsByTagName("document_name")[0].childNodes[0].nodeValue; 
        document.getElementById("documentList1").innerHTML = getData[1].getElementsByTagName("document_name")[0].childNodes[0].nodeValue; 
    
        console.log("done"); 
    } 
    

再添加一個跨度ID documentList1執行上面的代碼

+0

好的,謝謝很多,這是有效的,但我正在尋找它來顯示XML文件中的所有內容。我需要重複'document.getElementById(「documentList」)。innerHTML = getData [0] .getElementsByTagName(「document_name」)[0] .childNodes [0] .nodeValue; document.getElementById(「documentList1」)。innerHTML = getData [1]。getElementsByTagName(「document_name」)[0] .childNodes [0] .nodeValue;'文件中其他標記的行數? – 2013-03-20 12:29:14

+0

是的,你應該重複文件中的每個標籤 – 2013-03-20 12:35:57

+0

你應該重複標籤document_name,file_type和file_location,但你可以使用for循環來獲取文檔標籤下的元素..我的意思是循環代替getData [0],getData [1] ....等 – 2013-03-20 12:39:08