2013-07-25 3 views
0

.xml 확장자로 끝나지 않는 url에서 xml을 구문 분석 할 수 없습니다. 그러나 아래의 코드를 사용하면 처음/res/raw에 저장할 때 동일한 XML을 성공적으로 파싱 할 수 있습니다..xml 확장자없이 xml을 파싱 할 수 없습니다.

this 또는 "api.androidhive.info/pizza/?format=xml"와 같은 .xml 확장명으로 생성 된 경우 예를 들어.

또한 URL 끝에 /?format=xml을 추가해도 문제가 해결되지 않습니다.

나는 이것이 HttpResponse를 얻는 것과 관련이 있다고 생각했지만 현재 코드로 문제를 해결할 수 없다. xml을 검색하고 .xml 확장자 없이도 파싱 할 수 있어야합니다.

public String getXmlFromUrl(String url) { 
    String xml = null; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // return XML 
    return xml; 
} 

public Document getDomElement(String xml){ 
    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xml)); 
      doc = db.parse(is); 

     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 

     return doc; 
} 

여기에 내 로그 캣의 결과입니다 : 내가 서버의 응답을 선택하면

07-25 17:54:39.090: I/INFO(29276): Content:{"Message":"An error has occurred.","ExceptionMessage":"Value cannot be null.\r\nParameter name: entity","ExceptionType":"System.ArgumentNullException","StackTrace":" at System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires(Boolean condition, String userMessage, String conditionText)\r\n at System.Data.Entity.DbSet`1.Add(TEntity entity)\r\n at iisCMS.Controllers.GetAppsController.PostApp(App app)\r\n at lambda_method(Closure , Object , Object[])\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"} 

, 그것은 application/xml이었고, 나는 text/xml 다른 하나를 변환 할 수있는 방법이 필요하다고 생각?

+2

Logcat에서 로그를 올리십시오. –

+0

xml을 구문 분석 했습니까? –

+0

'xml'에서 무엇을 되찾았습니까? 분명히 XML이 아니므로 응답이 OK (200)가 아니 었는지 확인하고 리디렉션 (302)이 필요할 수 있습니다. URL의 POST가 올바른가요? 없어? 매개 변수가 없습니까? –

답변

0

주석에서 알 수 있듯이 올바른 XML을 문자열 변수 xml으로 읽었습니다.

먼저 파일을 XML로 작성하십시오.

Path path = new Path("C:/Temp/test.xml"); 
Files.write(file, xml.getBytes("UTF-8"), 
    StandardOpenOption.WRITE | StandardOpenOption.CREATE); 

그런 다음 브라우저에서 파일의 불규칙성을 확인할 수 있습니다. DTD, 스키마, 네임 스페이스, 유효성 검사.

관련 문제