简体   繁体   中英

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

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 .

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.

尝试将您的解析阅读器更改为:

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

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