2017-04-13 44 views
0

我知道這個話題是舊的。我正在學習和嘗試解析,但不知道如何去做。你們可以告訴我怎麼做,或者更好地幫助我獲得一個或兩個價值觀?用xml中的「count」將數據解析爲表格html

<Metainformation> 
    <cash>ABC XYZ</cash> 
    <Doctor>Dr. Peter Smith</Doctor> 
    <DoctorID>12345678</DoctorID> 
    <Quartal>Q22004</Quartal> 
    <Checkdate>20040404123000</Checkdate> 
</Metainformation> 
<Patientlist> 
    <Normal_Patient> 
     <Unchanged count="123"/> 
     <New count="3"/> 
     <Closed count="2"/> 
     <InTest count="4"/> 
    </Normal_Patient> 
    <Special_Patient> 
     <Special_Quantity count="8" /> 
    </Special_Patient> 
    <Notfound_Patient> 
     <ABC_available count="9" /> 
     <DEF_available count="7" /> 
    </Notfound_Patient> 
    <Total old="125" new="126"/> 
</Patientlist> 

我的表是在這裏: https://jsfiddle.net/n9ygoL52/

回答

1

OK,讓我們假設你有一個名爲infos.xml在HTML中存在相同目錄下的文件。

這個模式,我會告訴你,需要爲每個td設置ID。例如

<td>Cash</td> 
<td id="cash"> </td> 

<td>Doctor</td> 
<td id="doctor"> </td> 

<td>Quartal</td> 
<td id="quartal"> </td> 

,我使用的JavaScript ...

var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
    myFunction(this); 
    } 
}; 
xhttp.open("GET", "infos.xml", true); 
xhttp.send(); 

console.log(xhttp); 


function myFunction(xml) { 

    var cash = xml.responseXML; 
    document.getElementById("cash").innerHTML = 
    cash.getElementsByTagName("cash")[0].childNodes[0].nodeValue; 

    var doctor = xml.responseXML; 
    document.getElementById("doctor").innerHTML = 
    doctor.getElementsByTagName("Doctor")[0].childNodes[0].nodeValue; 

    var quartal = xml.responseXML; 
    document.getElementById("quartal").innerHTML = 
    quartal.getElementsByTagName("Quartal")[0].childNodes[0].nodeValue; 
} 

用同樣的方式,你可以添加表的所有TD。但它會更好,如果你使用一個循環來遍歷表和XML分別

投票,如果有幫助

更新

這種方式,我走了......

var unchanged = xml.responseXML; 

    var attrone= unchanged.getElementsByTagName('Unchanged'); 

    var c = 0; 

    for(var i in attrone){ 

     if(c == attrone.length) break; 

     c++;  
     document.getElementById('unchanged').innerHTML=attrone[i].getAttribute('count'); 

    } 
+0

非常感謝喬丹。我提出瞭解決方案,但我的問題也是關於'count'的數據(例如,來自'')。我怎樣才能解析'123'到我的桌子? – gnase

+0

正在尋找這個。看到你在你的xml中有錯誤。必須到文件末尾。總計在關閉標記處添加一個斜槓:) –

+0

謝謝,你是對的。我已經將'/'添加到Total中了。但我不認爲應該在最後。我認爲這個XML數據由兩部分組成:'Metainformation'和'Patientlist'。順便說一句,你能告訴我從'count'值的方法嗎? – gnase