使用phonegap / Jquery / Ajax将XML解析为数据库 [英] Parse an XML into a database with phonegap/Jquery/Ajax
问题描述
嘿大家我会非常感谢这里的一些帮助,我正在尝试解析XML并将结果放入我正在处理的应用程序的数据库中(Phonegap / JQuery移动应用程序)。有人可以告诉我如何在JS函数内执行此操作吗?
Hey everyone I was would greatly appreciate some help here, I am trying to parse an XML and put the results into a database for an app I am working on(Phonegap/JQuery mobile app). Can someone show me how to do this inside of a JS function?
我理解XML的解析过程但是我将它本地存储到SQLlite数据库中,而phonegap允许您访问它。这是我正在使用的XML:
I understand the parsing process for the XML however I am a little lost on storing it locally into the SQLlite database that phonegap lets you access. Here is the XML I am using:
<?xml version="1.0" encoding="UTF-8"?>
<orders>
<order>
<orderId>123456789</orderId>
<city>Cincinnati</city>
<state>Ohio</state>
<zip>45451</zip>
</order>
</orders>
这是一个解析这个的JS函数:
Here is a JS function to parse this:
$(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>');
});
}
});
});
谢谢大家!
推荐答案
创建数据库:
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)');
});
最好将INSERT置于AJAX的回调中:
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)');
});
});
}
});
祝你好运!
这篇关于使用phonegap / Jquery / Ajax将XML解析为数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!