2013-03-14 86 views
1
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, "&amp;").replace (/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g, "<br/>") + "<hr/>"; 
} 

這段代碼是xml到json的解析代碼。我遵循手機參考。但我的文件讀取器不會改變stateReady。那總是狀態0.請幫助我.. filereader.result方法在phonegap中不起作用? 我想回家。 :-(。我總是會看到這個帖子。Phonegap,ios filereader不起作用

+0

是OnDeviceReady由應用程序調用的?您可以通過在OnDeviceReady事件中添加一個簡單的警報來測試它。 – Whizkid747 2013-03-14 13:51:08

回答

0

嗯,你可能已經解決了,但如果對別人有用,你的回調無法正常鏈接,只是改變你符合這條線

fileSystem.root.getFile("ccw.xml", null, gotFile, fail); 

隨着

fileSystem.root.getFile("ccw.xml", null, gotFileEntry, fail`); 

試一下,也有助於爲您添加控制檯日誌消息,以便其更容易調試和理解發生了什麼以及以什麼順序。