简体   繁体   English

如何在循环的帮助下从 xml 文件动态获取表到 ajax?

[英]How can I fetch table from xml file to ajax dynamically with the help of loop?

I have this HTML but I did it manually and I need to do it dynamically and fetch the data from the XML file with the help of XMLHttpRequest in ajax.我有这个 HTML,但我是手动完成的,我需要动态完成并在 ajax 中的XMLHttpRequest的帮助下从 XML 文件中获取数据。 how can I get it within one function and loop through the data for the XML file?如何在一个函数中获取它并遍历 XML 文件的数据?

<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        h1,
        tr,
        th {
            text-align: center;
        }

        button {
            background-color: white;
            border: 0px;
        }
    </style>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>

    <div class="container col-sm-4">
        <h1 style="margin-bottom: 50px; text-align: center;">Ogrenciye tiklandiginda bilgilerin ekrana getirilmesi</h1>
        <table class="table">
            <thead class="thead-dark">
                <tr>
                    <th>Numara</th>
                    <th>AdSoyad</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><button>1111111</button></td>
                    <td><button>Dugu AKSEHIR</button></td>
                </tr>
                <tr>
                    <td><button>222222</button></td>
                    <td><button>Ayse CALISKAN</button></td>
                </tr>
                <tr>
                    <td><button>333333</button></td>
                    <td><button>Semih ARSLAN</button></td>
                </tr>
                <tr>
                    <td><button>444444</button></td>
                    <td><button>Mehemet ERKOC</button></td>
                </tr>
            </tbody>
        </table>
    </div>

</body>

</html>

which my xml file is like:我的 xml 文件是这样的:

<catalog>
    <student>
        <number>111111</number>
        <name>Dugu AKSEHIR</name>
        <sinif>3.sinif</sinif>
        <bolum>Bilgisayar Muhendisligi</bolum>
    </student>

    <student>
        <number>222222</number>
        <name>Dugu AKSEHIR</name>
        <sinif>2.sinif</sinif>
        <bolum>kimya Muhendisligi</bolum>
    </student>

    <student>
        <number>333333</number>
        <name>Semih ARSLAN</name>
        <sinif>1.sinif</sinif>
        <bolum>Harita Muhendisligi</bolum>
    </student>

    <student>
        <number>444444</number>
        <name>Mehemet ERKOC</name>
        <sinif>4.sinif</sinif>
        <bolum>Elektrik Muhendisligi</bolum>
    </student>
</catalog>

And the output should be something like this:输出应该是这样的:

You can read the whole file first and then traversing each student tag one by one you can keep appending to the table in the body.您可以先阅读整个文件,然后一个一个遍历每个学生标签,您可以继续附加到正文中的表格中。

        <div class="container">
        
        <table id="tableList" class="table col-xs-12  col-md-6"  >
            <thead class="thead-dark">
                <tr>
                    <th class="col-xs-6">Number</th>
                    <th class="col-xs-6">Name</th>
                </tr>
            </thead>
            <tbody>
                
            </tbody>
            
        </table>
    
    </div>

 
<script>

    $( document ).ready(function (){
        readXml('File.xml');     
    });

function readXml(xmlFile){
       var xmlDoc;
       if(typeof window.DOMParser != "undefined") {
           xmlhttp=new XMLHttpRequest();
           xmlhttp.open("GET",xmlFile,false);
           if (xmlhttp.overrideMimeType){
               xmlhttp.overrideMimeType('text/xml');
           }
           xmlhttp.send();
           xmlDoc=xmlhttp.responseXML;
       }
       else{
           xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
           xmlDoc.async="false";
           xmlDoc.load(xmlFile);
       }
        var tableRowData;
       $('student',xmlDoc).each(function(i){
               var tagObj=$(this);
               var numValue = tagObj[0].getElementsByTagName("number")[0].childNodes[0].nodeValue;
               var nameValue = tagObj[0].getElementsByTagName("name")[0].childNodes[0].nodeValue;
               console.log(numValue +', '+ nameValue);   
                tableRowData = "<tr><td>"+ numValue +" </td> <td>"+ nameValue +" </td> </tr>";
                $("#tableList tbody").append(tableRowData);
       });
 }        
    
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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