Android:第 1 行出现 parseException,column0 未找到元素?
我有一个问题,比如
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)
有时成功解析,但有时会出现上述错误。
我的代码是
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
任何人都可以帮我解决这个错误吗?
感谢悠悠, 斯里尼瓦斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否仔细检查过 XML 文件顶部没有空格,并且 XML 声明是最先出现的内容?
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?
而不是
inputStream = url.openStream();
xmlReader.parse(new InputSource(urlString));
尝试
xmlReader.parse(new InputSource(inputStream));
或
xmlReader.parse(new InputSource(urlString.openStream()));
instead of
inputStream = url.openStream();
xmlReader.parse(new InputSource(urlString));
try
xmlReader.parse(new InputSource(inputStream));
or
xmlReader.parse(new InputSource(urlString.openStream()));
我认为您由于 xml 格式不正确而面临这个问题...这是为您提供的解决方法。只需清理 xml,然后解析它...只需将 xml 的“&”替换为“& amp;”(忽略“& amp;”之间的空格)...这可能会解决您的问题。 ...因为此类问题可能由于“&”的出现而出现在获得的 XML 数据中。
例如:如果您的字符串 (xmlString ) 是您的 XML 数据,则
xmlString = xmlString.replaceAll( "&", "& amp;" );
[请忽略“&”之间的空格因为保存到表单时我无法生成相同的单词。 ]
将为您提供一个无错误的 XML 字符串进行解析。
希望它可以节省您至少几分钟的时间
问候,
苏丁·菲利普.
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.