KAREEM MAHAMMED
KAREEM MAHAMMED

Reputation: 1695

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!

Upvotes: 0

Views: 2405

Answers (2)

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 8856

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); 
}

Upvotes: 3

jonjbar
jonjbar

Reputation: 4066

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

Upvotes: 1

Related Questions