Reading XML and trying to insert in localSQLDB via javascript

Reading XML and trying to insert in localSQLDB via javascript

Hi,
I'm currently learning myself to build an HTML5 app with JQmobile and Jquery that later will be turned into a app via Phonegap. I've recently started to play with reading in items from a xml which then are supposed to be put into the local SQL lite db that you get access to in HTML5. 

The problem I have is that I do not seem to be able get the INSERT into the DB to work. I know I can read my XMl as I am able to output the contents fine on the page but when i use the same variable for the DB insert it just inserts the last item in the XMl over and over again.

My XML is very small and looks like this:

    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <NobelLaureates>
    3.   <Laureates>
    4.     <Name>Spock</Name>
    5.     <Category>Chemistry</Category>
    6.     <Year>2010</Year>
    7.   </Laureates>
    8.   <Laureates>
    9.     <Name>Kirk</Name>
    10.     <Category>Economics</Category>
    11.     <Year>2010</Year>
    12.   </Laureates> 
    13.   <Laureates>
    14.     <Name>Picard</Name>
    15.     <Category>Physics</Category>
    16.     <Year>2010</Year>
    17.   </Laureates> 
    18. </NobelLaureates>
my javascript-file that gets the xml and works on it looks like this:

  1. $(document).ready(function()
  2. {
  3.   $.ajax({
  4.     type: "GET",
  5.     url: "link_to_my_xml
  6.     success: function(xml) { parseXml(xml); }
  7.   });
  8. });

  9. function parseXml(xml)
  10. {
  11. var db = openDatabase('LaurDB', '1.0', 'LaurDB', 2 * 1024 * 1024);
  12. db.transaction(function (tx) {
  13.    tx.executeSql('CREATE TABLE IF NOT EXISTS laureates (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT, category TEXT, year INT)');
  14. tx.executeSql('DELETE FROM laureates');
  15. });
  16.    var Name = "";
  17.    var Category = "";
  18.    var Year = ""; 


  19.   //Create a SQL-database with all the info about the laureates
  20.   $(xml).find("Laureates").each(function()
  21.   {
  22.  
  23. Name = $(this).find("Name").text();
  24. Category = $(this).find("Category").text();
  25. Year = $(this).find("Year").text();
  26. //alert("namn:" + Name);
  27. $("#output").append(Name);
  28.     $("#output").append(": " + Category + " " + Year +"<br />");

  29. db.transaction(function (tx) {
  30. tx.executeSql('INSERT INTO laureates (name,category,year) VALUES (?,?,?)',[Name,Category,Year]);
  31. });
  32.   });
  33. }

    The odd this is that the variable Name, Category and Year indeed seems to get the correct content from the XMl as the #output-div where I append the debug info correctly lists each of the persons in the xml. However, in the database I only get the info for the last person (Picard), however 3 times!. I'm a bit confused here. Is something wrong with my function to get the content of the XML? Why does it work to append the correct info to the #output-div but in the next rows the same variable hasn't the same name? 

    I'm sure there's an easy explanation but I think I'm too tired to think of it right now .... 

    thanks in advance for any help.
      • Topic Participants

      • admin