var xml2;
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem){
fileSystem.root.getFile("ccw.xml", null, gotFile, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file) {
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(){
};
xml2 = reader.result;
writeJson();
}
function fail(evt) {
console.log(evt.target.error.code);
}
function parseXml(xml) {
var dom = null;
if (window.DOMParser) {
try {
dom = (new DOMParser()).parseFromString(xml, "text/xml");
}
catch (e) { dom = null; }
}
else if (window.ActiveXObject) {
try {
dom = new ActiveXObject('Microsoft.XMLDOM');
dom.async = false;
if (!dom.loadXML(xml)) // parse error ..
window.alert(dom.parseError.reason + dom.parseError.srcText);
}
catch (e) { dom = null; }
}
else
alert("oops");
return dom;
}
function writeJson(){
var json;
// for(var i = 0 ; i < xml.length ; i++) {
show("\n\n" + (json = xml2json(parseXml(xml2), " ")) + "\n\n");
// console.log("====="+i+"/"+xml.lenght+"=====");
// }
}
function show(s) {
document.getElementById("out").innerHTML += (s+"\n").replace(/&/g, "&").replace (/</g,"<").replace(/>/g,">").replace(/\n/g, "<br/>") + "<hr/>";
}
此代码是 xml 到 json 解析代码。我遵循phonegap参考。但我的文件阅读器不会改变 stateReady。那总是状态 0。请帮帮我.. filereader.result 方法在 phonegap 中不起作用?我想回家。:-( .i 将永远看到这篇文章。