开发者

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.printStac开发者_StackOverflowkTrace();
    }

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.


try to change your parse reader become like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜