Reputation: 25
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. I want to load parts of my xml file into 'contents' div.
This is my HTML Code :
<div>
<input type="button" id="click"/>
<div id="content">
</div>
</div>
This is my 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. (Sorry if I messed up explaining it ! :) ).
Upvotes: 0
Views: 1049
Reputation: 3426
It is possible to load the xml using jQuery with $.ajax
, $.get
, $.post
.
Once you are using jQuery it has a method $.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
Upvotes: 1