Parse an XML into a database with phonegap/Jquery/Ajax

旧街凉风 提交于 2019-12-08 09:03:36

Create the DB:

var db = openDatabase('PhoneGap_db', '1.0', '', 2 * 1024 * 1024);

Create the table:

db.transaction(function (tx) {
  tx.executeSql('CREATE TABLE IF NOT EXISTS orders (id unique, city, state, zip)');
});

Insert into table:

db.transaction(function (tx) {
  tx.executeSql('INSERT INTO orders (id, city, state, zip) VALUES (orderId,city,state,zip)');
});

it will be best to put the INSERT inside the AJAX's callback:

$.ajax({
    type: "GET",
    url: "testOrders.xml",
    dataType: "xml",
    success: function(xml) {
      $(xml).find('order').each(function(){
        orderId = $(this).find("orderId").text();
        city = $(this).find("city").text();
        state = $(this).find("state").text();
        zip = $(this).find("zip").text();
        db.transaction(function (tx) {
           tx.executeSql('INSERT INTO orders (id, city, state, zip) VALUES      (orderId,city,state,zip)');
        });
            });
        }
     });

Good luck!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!