2012-12-06 26 views
1

大家好,我在這裏非常感謝一些幫助,我試圖解析XML,並把結果放到了一個應用程序,我工作的一個數據庫(的PhoneGap/JQuery移動應用程序)。有人可以告訴我如何在JS函數中執行此操作嗎?解析XML與數據庫中的PhoneGap/jQuery的/阿賈克斯

我瞭解XML解析過程不過我有點失去了在本地存儲至SQLlite數據庫的PhoneGap可以讓你訪問。下面是我使用的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<orders> 
    <order> 
     <orderId>123456789</orderId> 
     <city>Cincinnati</city> 
     <state>Ohio</state> 
     <zip>45451</zip> 
    </order> 
</orders> 

下面是一個JS函數來分析這樣的:

 $(document).ready(function(){ 
    $.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(); 
     $("#acceptedOrdersContent").append('<div>'); 
     $("#acceptedOrdersContent").append('<h3>'OrderId: '+ orderId + '</h3>'); 
     $("#acceptedOrdersContent").append('<p>'); 
     $("#acceptedOrdersContent").append('City: '+ city + '<br />'); 
     $("#acceptedOrdersContent").append('State: '+ state + '<br />'); 
     $("#acceptedOrdersContent").append('Zip: '+ zip +'<br />'); 
     $("#acceptedOrdersContent").append('</p>'); 
     $("#acceptedOrdersContent").append('</div>'); 
      }); 
     } 
    }); 
}); 

謝謝大家!

回答

5

創建DB:

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

創建該表:

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

插入到表:

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

這將是最好把插入AJAX的回調裏面:

$.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)'); 
     }); 
      }); 
     } 
    }); 

祝你好運!

+0

dod_basim感謝您的快速回復,我確實對您有另一個問題。我現在如何顯示這些數據在數據庫中? – user1857654