xml confusion
xml confusion
Ok am playing with xml to see what I can do with it, was working fine with one variable however as soon as I added a second one:
var author_text = $(xml.author).text();
It seems to be incrementally adding the data on each loop - can anyone point out how I can fix this?
JQuery:
-
$.ajax({
url: 'source.xml',
type: 'GET',
dataType: 'xml',
timeout: 1000,
error: function(){
alert('XML Function Error 1.1');
},
success: function(xml){
// do something with xml
$(xml).find('title').each(function(){
var title_text = $(this).text();
var author_text = $(xml.author).text();
$('<li></li>')
.html(title_text +" - "+ author_text)
.appendTo('ol');
});
}
});
XML file:
-
<?xml version="1.0" encoding="UTF-8"?>
<bestsellers>
<book>
<title>
<![CDATA[
Plum Lovin' (A Stephanie Plum Novel)
]]>
</title>
<author>
<![CDATA[
Janet Evanovich
]]>
</author>
<publisher>
<![CDATA[
St. Martin's Press
]]>
</publisher>
<isbn>
<![CDATA[
0312306342
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Cross
]]>
</title>
<author>
<![CDATA[
James Patterson
]]>
</author>
<publisher>
<![CDATA[
Little, Brown and Company
]]>
</publisher>
<isbn>
<![CDATA[
0316159794
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
For One More Day
]]>
</title>
<author>
<![CDATA[
Mitch Albom
]]>
</author>
<publisher>
<![CDATA[
Hyperion
]]>
</publisher>
<isbn>
<![CDATA[
1401303277
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
The Hunters
]]>
</title>
<author>
<![CDATA[
W.E.B. Griffin
]]>
</author>
<publisher>
<![CDATA[
Putnam Adult
]]>
</publisher>
<isbn>
<![CDATA[
0399153799
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Shadow Dance: A Novel
]]>
</title>
<author>
<![CDATA[
Julie Garwood
]]>
</author>
<publisher>
<![CDATA[
Ballantine Books
]]>
</publisher>
<isbn>
<![CDATA[
0345453867
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Exile: A Novel
]]>
</title>
<author>
<![CDATA[
Richard North Patterson
]]>
</author>
<publisher>
<![CDATA[
Henry Holt and Co.
]]>
</publisher>
<isbn>
<![CDATA[
0805079475
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Next
]]>
</title>
<author>
<![CDATA[
Michael Crichton
]]>
</author>
<publisher>
<![CDATA[
Henry Holt and Co.
]]>
</publisher>
<isbn>
<![CDATA[
HarperCollins
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Stalemate (Eve Duncan Forensics Thrillers)
]]>
</title>
<author>
<![CDATA[
Iris Johansen
]]>
</author>
<publisher>
<![CDATA[
Bantam
]]>
</publisher>
<isbn>
<![CDATA[
055380345X
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Dear John
]]>
</title>
<author>
<![CDATA[
Nicholas Sparks
]]>
</author>
<publisher>
<![CDATA[
Bantam
]]>
</publisher>
<isbn>
<![CDATA[
0446528056
]]>
</isbn>
</book>
<book>
<title>
<![CDATA[
Hannibal Rising
]]>
</title>
<author>
<![CDATA[
Thomas Harris
]]>
</author>
<publisher>
<![CDATA[
Delacorte Press
]]>
</publisher>
<isbn>
<![CDATA[
0385339410
]]>
</isbn>
</book>
</bestsellers>