使用jquery将xml文件内容加载到div中 [英] Load xml file content into div using jquery

查看:101
本文介绍了使用jquery将xml文件内容加载到div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将xml文件内容加载到div.

How to load the xml file content to a div.

这是我的HTML,需要在其中加载XML内容

Here is my HTML in which I need to load XML content

<div class="editorial" id="news-container">
</div>

XML数据

<contentprogramming>
    <category name = "My t" id = "1">
        <logo>http://123.png</logo>        
        <channel name="res" id= "1" type="ecommerce">            
            <logo>http://11.png</logo>           
            <items>              
                <item id="1">
                    <image>http://11.jpg</image>
                    <title>Memory Card MSMT2G</title>
                    <details>$6.99</details>
                    <linkurl>http://www.test.com/Sony</linkurl>
                </item>
                <item id="2">
                    <image>http://i2.jpg</image>
                    <title>Apple iPad </title>
                    <details>$579.95</details>
                    <linkurl>http://www.test.com/Apple</linkurl>
                </item> 
            </items>
        </channel>
    </category>
</contentprogramming>

并且xml文件名为something.xml

我尝试了几种方法,但是没有用:(

I tried several ways but it didn't work :(

我尝试了以下方法,但是没有用.

I tried the below method but didn't work.

$('something.xml').find('name').each(function() { 
    $("#news-container").append($(this).text() + "<br />");
}); 

推荐答案

尝试一下:

// Load the xml file using ajax 
$.ajax({
    type: "GET",
    url: "something.xml",
    dataType: "xml",
    success: function (xml) {

        // Parse the xml file and get data
        var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);
        $xml.find('category[name="My t"] logo').each(function () {
            $("#news-container").append($(this).text() + "<br />");
        });
    }
});

参考:1. jQuery.ajax() 2.

Reference: 1. jQuery.ajax() 2. parseXML()

这篇关于使用jquery将xml文件内容加载到div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆