简体   繁体   中英

How to parse a local XML file in Titanium?

My Resources folder contains an XML file. I need to parse it in Titanium. I have written the following code:

try {
    var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'Translation.xml');
    var xmltext = file.read().text;
    var doc = Ti.XML.parseString(xmltext);
}
catch(e) {
     alert(e); 
     Ti.API.info(e);    
}

But I am getting the next error:

 - result of expression 'file.read() is not an object

Any solution? Thanks!

Try to check if your file exists or not.

var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'Translation.xml'); 
if ( file.exists() ) {
        var xmltext = file.read().text;
        var doc = Ti.XML.parseString(xmltext); 
}

It looks like the file can't be found on the system, that's why you are getting the error. Try putting the whole path as mentioned bellow. Example:

var file = Titanium.Filesystem.getFile("../Resources/tableWindows/CrossRef.xml");

Somebody with the same problem: http://developer.appcelerator.com/question/123246/xml-file-will-not-read

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM