0

这是正在使用的脚本...我在按钮单击时调用了 copyxml 函数...警报消息 alert("writen") 来了。但是当我打开我的 xml 文件时,xml 文件中不存在数据。我在 android 模拟器中做这一切。

    function copyxml() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null);
        function onRequestFileSystemSuccess(fileSystem) {
            var entry = fileSystem.root;
            entry.getDirectory("DigitalDirectory", { create: true, exclusive: false }, onGetDirectorySuccess, onGetDirectoryFail);
        };
        function onGetDirectorySuccess(dir) {
            //alert("Created dir " + dir.name);
            dir.getDirectory("DatabasesXML", { create: true, exclusive: false }, CreateDB, onGetDirectoryFail);
        };
        function CreateDB(dir) {
            alert("created directory " + dir.name);
            dir.getFile("users.xml", { create: true, exclusive: false }, gotFileEntry, fail);
            function gotFileEntry(fileEntry) {
                alert("file created");
                fileEntry.createWriter(gotFileWriter, fail);
                //fileEntry.file(gotFile, fail);
            };
            function gotFileWriter(writer) {
                writer.seek(writer.length);
                writer.write("Hi");
                alert("writen");
                writer.onerror = function(error) {

                };
            };
            function fail(msg) {
                alert("Error occured:");
            };
        };
        function onGetDirectoryFail(error) {
            alert("Error creating directory " + error.code);
        };
    }
    //...............reading xml file
    function readxml() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null);
        function onRequestFileSystemSuccess(fileSystem) {
            var entry = fileSystem.root;
            entry.getDirectory("DigitalDirectory", { create: true, exclusive: false }, onGetDirectorySuccess, onGetDirectoryFail);
        };
        function onGetDirectorySuccess(dir) {
            //alert("Created dir " + dir.name);
            dir.getDirectory("DatabasesXML", { create: true, exclusive: false }, CreateDB, onGetDirectoryFail);
        };
        function CreateDB(dir) {
            alert("created directory " + dir.name);
            dir.getFile("users.xml", { create: true, exclusive: false }, gotFileEntry, fail);
        };
        function gotFileEntry(fileEntry) {
            alert("Got file " + fileEntry.name);
            fileEntry.file(win, fail);
            function win(file) {
                var reader = new FileReader();
                reader.onloadend = function(evt) {
                    alert("read success");
                    var xml = evt.target.result;
                    alert(xml);
                    fileObject = xml;
                    ParseXmlCat(xml);
                    //                        user = xml.getElementsByTagName("users");
                    //                        for (i = 0; i < username.length; i++) {
                    //                            var username = user[i].getElementsByTagName("username").nodeValue;
                    //                            alert(username);
                    //                        }
                    function ParseXmlCat(xmlcat) {
                        alert(xmlcat);
                        $(xmlcat).find("user").each(function() {
                            var name = $(this).attr("username");
                            var displayName = $(this).attr("password");
                            alert(name);
                            alert(displayName);
                            //console.log(name);
                            //var litext = "<li class='licat'><a href='video.html' id='" + name + "'>" + displayName + "</a></li>";
                            //console.log(litext);
                            //$("#category").append(litext);
                        });
                        //$('ul').listview('refresh');
                    };
                };
                reader.readAsText(file)
            };
        };
        function fail(msg) {
            alert("Error occured:");
        };
        function onGetDirectoryFail(error) {
            alert("Error creating directory " + error.code);
        };
    }
</script>
4

0 回答 0