简体   繁体   English

使用jQuery获取XML数据

[英]Getting XML Data with jQuery

I can not write XML data into my HTML page. 我无法将XML数据写入HTML页面。 It seems it's not even reading, because it doesn't even alert. 似乎它甚至都没有阅读,因为它甚至没有警报。 What part am I missing ? 我缺少哪一部分?

My XML: 我的XML:

<ProgramOgrenmeCiktilari>

    <Category>
        <TopTitle> Bilgi </TopTitle>
        <Title> Kuramsal, Olgusal </Title>
        <Text>- Matematik, fen bilimleri ve ilgili mühendislik disiplinine özgü konularda yeterli bilgi birikimi;...
             </Text>
    </Category>

    <Category>
        <TopTitle> Beceriler </TopTitle>
        <Title> Bilişsel, Uygulamalı </Title>
        <Text>- Karmaşık bir sistemi, süreci, cihazı veya ürünü gerçekçi kısıtlar ve koşullar altında,...
            </Text>
    </Category>

    <Category>
        <TopTitle> Yetkinlikler </TopTitle>
        <Title> Bağımsız Çalışabilme ve Sorumluluk Alabilme Yetkinliği </Title>
        <Text>- Disiplin içi ve çok disiplinli...</Text>
    </Category>

</ProgramOgrenmeCiktilari>

jQuery part jQuery部分

$(document).ready(function () {
    /* here goes some other codes about other buttons... */

    $("#program").click(function () {
        $.get('ProgramOgrenmeCiktilari.xml', function (data) {
            $(xml).find('Category').each(function () {
                var TopTitle = $(this).find('TopTitle').text();
                var Title = $(this).find('Title').text();
                var Text = $this.find('Text').text();

                var html = '<tr>   <td colspan="3" style="background-color:#DFE4FF;"><b>TopTitle     </b></td> </tr> ';
                html += '<tr> <td><br> <b>Title</b> <br><br>Text <br><br></td></tr><tr>';

                $(".kutu_icerik").html(data);
            });
        });
    });
});

if response is 200 ok 如果响应为200 ok
It be $(data).find('Category').each(function(){ instead of $(xml).find('Category').each(function(){ 它是$(data).find('Category').each(function(){而不是$(xml).find('Category').each(function(){
and

var html = '<tr><td colspan="3" style="background-color:#DFE4FF;"><b>+'TopTitle'+</b>/td> </tr> ';
html += '<tr> <td><br> <b>'+Title+'</b> <br><br>='+Text'+<br><br></td></tr>';
$(".kutu_icerik").html(html);  

instead of $(".kutu_icerik").html(data); 而不是$(".kutu_icerik").html(data);

I suggest you work with an HTML templating library like Handlebars . 我建议您使用诸如Handlebars之类的HTML模板库。 These libraries help you generate HTML from structured data (objects and arrays). 这些库可帮助您从结构化数据(对象和数组)生成HTML。

Sou your task would be to generate objects and arrays from the input XML and write a basic template that handlebars can use. 例如,您的任务将是从输入XML生成对象和数组,并编写一个可供手把使用的基本模板。 Handlebars will then do the tedious part of HTML generation. 然后,把手将完成HTML生成的繁琐部分。

Advantages: 好处:

  • Much cleaner JS code. 更清晰的JS代码。
  • Much less to to go wrong (especially when it comes to escaping data properly). 要减少的错误要少得多(尤其是在正确转义数据时)。
  • HTML templates live where all your other HTML is. HTML模板位于您所有其他HTML所在的位置。

 // prepare the template (you only need to do this once) var categoryTemplate = Handlebars.compile($("#Category-template").html()); // this is what the server returns var xml = "<ProgramOgrenmeCiktilari>\\ <Category>\\ <TopTitle> Bilgi </TopTitle>\\ <Title> Kuramsal, Olgusal </Title>\\ <Text>- Matematik, fen bilimleri ve ilgili mühendislik disiplinine özgü \\konularda yeterli bilgi birikimi;...\\ </Text>\\ </Category>\\ <Category>\\ <TopTitle> Beceriler </TopTitle>\\ <Title> Bilişsel, Uygulamalı </Title>\\ <Text>- Karmaşık bir sistemi, süreci, cihazı veya ürünü gerçekçi kısıtlar ve koşullar altında,...\\ </Text>\\ </Category>\\ <Category>\\ <TopTitle> Yetkinlikler </TopTitle>\\ <Title> Bağımsız Çalışabilme ve Sorumluluk Alabilme Yetkinliği </Title>\\ <Text>- Disiplin içi ve çok disiplinli...</Text>\\ </Category>\\ </ProgramOgrenmeCiktilari>"; // in the Ajax success callback... var categories = $(xml).find('Category').map(function () { return { TopTitle: $(this).find('TopTitle').text(), Title: $(this).find('Title').text(), Text: $(this).find('Text').text() }; }).toArray(); $(".kutu_icerik tbody").html(categoryTemplate(categories)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script id="Category-template" type="text/x-handlebars-template"> {{#each .}} <tr> <td colspan="3" style="background-color:#DFE4FF;"> <b>{{TopTitle}}</b> </td> </tr> <tr> <td></td> <td colspan="2"> <b>{{Title}}</b> <br> <br>{{Text}} </td> </tr> {{/each}} </script> <table class="kutu_icerik"> <thead> <tr> <th>Col1</th><th>Col2</th><th>Col3</th> </tr> </thead> <tbody> </tbody> </table> 

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

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