[英]Load in xml file using jQuery
I've been browsing through stackoverflow for a while and have not found an answer that suits my problem - I want to load in a certain part of my xml file when I click a button. 我一直在浏览stackoverflow一段时间,但没有找到适合我的问题的答案-我想在单击按钮时加载xml文件的特定部分。 I want to load parts of my xml file into 'contents' div.
我想将我的xml文件的一部分加载到“目录” div中。
This is my HTML Code : 这是我的HTML代码:
<div>
<input type="button" id="click"/>
<div id="content">
</div>
</div>
This is my XML : 这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<books>
<book id="jeff">
<author>Jeffrey </author>
<title>Books 1</title>
</book>
<book id="jerry">
<author>Jerry</author>
<title>Books 2</title>
</book>
<book id="jimmy">
<author>Jimmy</author>
<title>Boks 3</title>
</book>
<book id="jem">
<author>Jem</author>
<title>Books 4</title>
</book>
</books>
I have tried this (I know this is far from right - so no hate please) : 我已经尝试过了(我知道这很不正确-所以请不要讨厌):
$(document).ready(function() {
$('#click').click(function(){
$.ajax({
url : 'books.xml',
dataType : 'xml',
success : parse,
});
function parse(xml){
$('#content').append($(xml).find('authour').text());
}
});
});
How would I access each authours id specification using jQuery. 我将如何使用jQuery访问每个authids规范。 (Sorry if I messed up explaining it ! :) ).
(对不起,如果我搞砸了!!))。
It is possible to load the xml using jQuery with $.ajax
, $.get
, $.post
. 可以使用带有
$.ajax
, $.get
和$.post
jQuery加载xml。 Once you are using jQuery it has a method $.parseXML
: 一旦使用jQuery,它就有一个方法
$.parseXML
:
$.ajax({url: 'file.xml', method: 'GET', dataType: 'xml'}).done(function (xml) {
var xmlDoc = $.parseXML( xml );
var author = $(xmlDoc).find('book[id="jem"]').find('author');
$('#content').append('<span>' + author.text() + '</span>');
});
The server should respond with the correct mime type Content-type: "text/xml"
in this case 在这种情况下,服务器应使用正确的mime类型
Content-type: "text/xml"
响应Content-type: "text/xml"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.