2013-03-06 1 views
4

파징 RSS의 시작 "예기치 않은 토큰"예외가 발생합니다 합법적 인 RSSKXmlParser 내가이 URL을 사용하여 안드로이드 17 절에 괴물에서 RSS 피드를 구문 분석하려고 해요

Cache-Control:private 
Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Length:5958 
Content-Type:text/xml 
Date:Wed, 06 Mar 2013 17:15:20 GMT 
P3P:CP=CAO DSP COR CURa ADMa DEVa IVAo IVDo CONo HISa TELo PSAo PSDo DELa PUBi BUS LEG PHY ONL UNI PUR COM NAV INT DEM CNT STA HEA PRE GOV OTC 
Server:Microsoft-IIS/7.5 
Vary:Accept-Encoding 
X-AspNet-Version:2.0.50727 
X-Powered-By:ASP.NET 
012 돌아 오기까지 내가 말할 수있는 무엇 다음과 같은 방식으로

this.conn = (HttpURLConnection) url.openConnection(); 
this.conn.setConnectTimeout(5000); 
this.conn.setReadTimeout(10000); 
this.conn.setUseCaches(true); 
conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
is = new InputStreamReader(url.openStream()); 

에 HttpURLConnection의를 사용하여 (내가 너무 그것을 확인) 있어요 3,516,는

그것은 (당신이 완전한 XML을보고 싶다면 위의 URL을 클릭)과 같이 시작합니다

final XmlPullParser xpp = getPullParser(); 
xpp.setInput(is); 
for (int type = xpp.getEventType(); type != XmlPullParser.END_DOCUMENT; type = xpp.next()) { /* pasing goes here */ } 

코드는 즉시로 type = xpp.next()에 질식 :

<?xml version="1.0" encoding="utf-8"?> 
<rss version="2.0"> 
    <channel> 
    <title>Monster Job Search Results java</title> 
    <description>RSS Feed for Monster Job Search</description> 
    <link>http://rss.jobsearch.monster.com/rssquery.ashx?q=java</link> 

을하지만 난 그것을 구문 분석 할 때 다음 예외가 발생했습니다

03-06 09:27:27.796: E/AbsXmlResultParser(13363): org.xmlpull.v1.XmlPullParserException: 
    Unexpected token (position:TEXT @1:2 in [email protected]) 

사실 제 1 행의 두 번째 문자는 처리 할 수 ​​없습니다. <?xml version="1.0" encoding="utf-8"?>

다음은 KXmlParser.java (425-426)의 잘못된 행입니다. 타입 ==의 텍스트는 true

if (depth == 0 && (type == ENTITY_REF || type == TEXT || type == CDSECT)) { 
    throw new XmlPullParserException("Unexpected token", this, null); 
} 

어떤 도움을 평가? 내가 XmlPullParser.FEATURE_PROCESS_DOCDECL = false로 파서를 설정하려고 않았다하지만 여기에 내가 웹에 대한 연구에게 이런 짓을

도움이되지 않았고

답변

34

당신이 오류가 발생하는 이유를하는 데 도움이 아무것도 찾을 수없는 것은 XML이다 파일은 실제로 <?xml version="1.0" encoding="utf-8"?>로 시작하지 않습니다.의 세 특수 바이트로 시작하여 Byte order mark입니다. 사용자가 수동으로 처리 할 수 ​​있도록

Hex representation

InputStreamReader는 자동으로 이러한 바이트를 처리하지 않습니다. 그것에 가장 간단한 방법은 Commons IO 라이브러리에서 사용할 수 BOMInpustStream을 사용하는 것입니다

this.conn = (HttpURLConnection) url.openConnection(); 
this.conn.setConnectTimeout(5000); 
this.conn.setReadTimeout(10000); 
this.conn.setUseCaches(true); 
conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
is = new InputStreamReader(new BOMInputStream(conn.getInputStream(), false, ByteOrderMark.UTF_8)); 

나는 위의 코드를 확인했는데 나를 위해 잘 작동합니다.

+2

정확히 내가 Stackoverflow를 사랑하는 이유입니다! 사람은 항상 자신보다 더 똑똑한 사람을 찾을 수 있습니다! 잘받을 가치가있는 현상금 (비록 내가 내일보다 빨리 그것을 수여 할 수는 없지만)! 고맙습니다! – Bostone

+0

메신저는이 오류가 있지만 문자열 변수에 xml이 있는데 무엇을 할 수 있습니까? – Bachask8

+5

또는 data.replaceAll ("^. * <", "<") Works 할 수 있습니다. –