简体   繁体   中英

Parse data with “count” from xml into table html

I know this topic is old. I am learning and trying to parse, but don't know how to do it in my case. Could you guys show me the way to do it or better help me with one or two values?

<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>

My table is here: https://jsfiddle.net/n9ygoL52/

ok, lets assume that you have a file called infos.xml in the same directory where html exist.

this mode that i will show you, requires to set ids for each of td. For example

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

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

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

the javascript that i use...

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;
}

with the same way you can add all td of table. But it will much better if you use a loop to iterate the table and xml respectively

vote if it helps

An update

with this way i take the count...

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');

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM