Srinivas
Srinivas

Reputation: 1736

How to parse xml from sdcard using sax parser in android?

I have an issue while parsing a local xml file using sax parser . It throwing an exception while parsing.

 *org.apache.harmony.xml.ExpatParser$ParseException: At line 7, column 247: not well-formed (invalid token)
org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:507)
org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492)
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308)
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264)*


try {
        String path = Environment.getExternalStorageDirectory() + "/FlightGuide/"+stateName+"/contents.xml"; 
        File file = new File(path);
        SAXParserFactory parserFactory=SAXParserFactory.newInstance();
        SAXParser saxParser=parserFactory.newSAXParser();
        XMLReader reader=saxParser.getXMLReader();
        AirportHandler airportHandler = new AirportHandler();
        reader.setContentHandler(airportHandler);
        reader.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));
        statePackage = airportHandler.getpaStatePackage();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace(); 
    } catch (IOException e) {

        e.printStackTrace();
    }

Please can any one figure out mistake I am doing? And is there any other method to parse local xml files? If so give me some sample code.

Thanking you, Srinivas

Upvotes: 1

Views: 3747

Answers (3)

ahilmawan
ahilmawan

Reputation: 1

try to change your parse reader become like this:

reader.parse(new InputSource(new FileInputStream(file)));

Upvotes: 0

dstefanox
dstefanox

Reputation: 2222

There are several possible ways to parse XML on Android, and they are well explained with nice examples in this article. As said in previous answer, your XML is invalid (has invalid token inside). You also may try to open your XML file in browser which will in most cases report error in badly formed XML.

Upvotes: 0

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

Your parsing code seems to be valid, the xml is incorrect. Please, check the 7th line of the contents.xml. Try to validate it with online xml validators.

Upvotes: 0

Related Questions