如何将xml文件内容加载到div.
这是我需要加载XML内容的HTML
<div class="editorial" id="news-container">
</div>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
而xml文件名是 something.xml
我尝试了几种方法,但它没有用:(
我尝试了下面的方法,但没有奏效.
$('something.xml').find('name').each(function() {
$("#news-container").append($(this).text() + "<br />");
});
Run Code Online (Sandbox Code Playgroud)
pal*_*aѕн 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 />");
});
}
});
Run Code Online (Sandbox Code Playgroud)
参考:1 jQuery.ajax() 2. parseXML()