I have an issue like
o
org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found
org.apache.harmony.xml.开发者_开发知识库ExpatParser.finish(ExpatParser.java:553) Displayed activity com.logictreeit.flight1/com.logictreeit.flight.app.MainTabActivity: 1689 ms (total 1689 ms)
org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:483)
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:320)
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:293)
Some times successfully parsing , but some times it thrwoing above error.
My code is
URL url = new URL(urlString);
SAXParserFactory saxParserFactory= SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
FlightGuideHandler flightGuideHandler = new FlightGuideHandler();
xmlReader.setContentHandler(flightGuideHandler);
inputStream = url.openStream();
xmlReader.parse(new InputSource(urlString)); // am getting error at this line
Can any one help me to sort out this error .
Thanking yoyu , Srinivas
Have you double checked that there is no white space at the top of the XML file and the XML declaration is the very first thing that appears?
instead of
inputStream = url.openStream();
xmlReader.parse(new InputSource(urlString));
try
xmlReader.parse(new InputSource(inputStream));
or
xmlReader.parse(new InputSource(urlString.openStream()));
I think you are facing this issue due to xml that is not in good format...Here is a work around for you. Just clean up xml, before parsing it....just replace xml's '&', with "& amp;"(ignore the space between "& amp;")..... this may fix your issue.... as this kinds of issue could arrise due to appearance of '&' in the XML data obtained.
eg: if your string (xmlString ), is your XML data, then
xmlString = xmlString.replaceAll( "&", "& amp;" ); [please ignore the space between "& amp;" as i could not produce the same word when saved to the form. ]
will give you an error free XML string for parsing.
Hope it may saved your time atleast few minutes
Regards, Sudhin Philip.
精彩评论