使用jquery将xml文件内容加载到div中

时间:2013-05-07 10:51:57

标签: jquery xml load

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

这是我需要加载XML内容的HTML

<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

我尝试了几种方法,但它不起作用:(

我尝试了以下方法,但没有奏效。

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

5 个答案:

答案 0 :(得分:19)

试试这个:

// 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. parseXML()

答案 1 :(得分:3)

您需要转义HTML特殊字符。在将文本设置为div之前使用此功能。

function htmlSpecialChars(unsafe) {
    return unsafe
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");
}

答案 2 :(得分:2)

- 您必须先调用xml文件

    $.ajax({
       url: '/something.xml',
       type: "GET",
       dataType: "xml",
       success: function (result) {
            $(result).find('name').each(function () {
                $("#news-container").append($(this).text() + "<br />");
             }
           }
       });

答案 3 :(得分:2)

如果您想要像我一样阅读本地文件,您可以执行以下操作:

<input type="file" id="file" accept="text/xml">
<script type="text/javascript">
document.getElementById('file').addEventListener('change', function (evt) {
    if (!evt.target.files){
        return;
    }

    var file = evt.target.files ? evt.target.files[0] : null,
        reader = new FileReader();

    reader.onload = function (evt) {
        console.log(evt.target.result);
    };

    reader.readAsText(file);
});
</script>

这要求用户选择文件,但它确实可以获取文件的内容。它使用HTML5 FileReader API

答案 4 :(得分:0)

试试这个:

var 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>';
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$xml.find( "category[name='My t'] logo" ).each(function () {
    $("#news-container").append($(this).text() + "<br />");
}
相关问题