【问题标题】:Read local file in phonegap android application在phonegap android应用程序中读取本地文件
【发布时间】:2012-07-13 09:42:45
【问题描述】:

在我的 phonegap android 应用程序中,我在 assets/www/ 文件夹中有一个 xml 文件。如何在 javascript 中读取该文件的内容?

【问题讨论】:

    标签: javascript cordova phonegap-plugins


    【解决方案1】:

    尝试使用这个库http://www.fyneworks.com/jquery/xml-to-json/,它将xml数据转换为易于使用的json。

    这个函数会加载xml文件:

    function readXml(){
            $.get('data.xml', function(xml){
    
                // converts received XML document to string
                var xmlText = new XMLSerializer().serializeToString(xml);
                console.log(xmlText);
    
                var employees = $.xml2json(xml);
                console.log(employees)
                alert(employees.employee[0].name);
            });
        }
    

    完整来源 - https://gist.github.com/3106218

    使用 Cordova 1.7 和 Android 2.2 对此进行了测试

    【讨论】:

    • 感谢您的回复。但我不想转换为 json。我只想要 xml 内容,或者您​​可以考虑使用任何文本文件。那怎么读呢?有办法吗?
    • 上述函数读取任何文件,一旦数据可用,然后我启动 xml 到 json 转换
    • $.get('metadata.xml', function(xml){ console.log(xml.toString()); });这将打印 [Object Document] 但我希望整个 xml 内容作为字符串。如何从这个对象中取出字符串?
    猜你喜欢
    • 2016-07-18
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2015-09-30
    • 2011-09-19
    • 1970-01-01
    • 2017-08-20
    • 2014-03-04
    相关资源
    最近更新 更多