2011-05-16 1 views
0

Xml parse api가 sax 구문 분석 예외를 던지고 있습니다. 루트 노드에 특성이있는 XML 파일을 구문 분석하려고하면.안드로이드 3.1 SDK에서 UTF-8로 인코딩 된 XML 파일의 구문 분석에 문제가 발생했습니다.

내가 알아 차 렸던 한 가지는 문자열의 시작 부분에 UTF-8 BOM 문자가있는 경우 발생합니다. BOM 문자를 제거하면 문제가 없습니다. 이 코드는 3.0 SDK에서 잘 작동되며, 아래, 나는 다음과 같은 파서 사용하고 있습니다 만

3.1에서이 문제를보고이 시도

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = docFactory.newDocumentBuilder(); 

    Document doc = null; 
    StringReader sr = new StringReader(xmlString); 
    InputSource is = new InputSource(sr); 

    doc = builder.parse(is); 

답변

0

을 :

public Document parse(String xml) throws ParsingFailedException { 
    try { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 

     //encode the xml to UTF -8 

     ByteArrayInputStream encXML = new ByteArrayInputStream(xml.getBytes("UTF8")); 

     Document doc = builder.parse(encXML); 
     log.error("XML parsing OK"); 

     return doc; 
    } catch (Exception e) { 
     log.error("Parser Error:" + e.getMessage()); 
     throw new ParsingFailedException("Failed to parse XML : Document not well formed", e); 
    } 
} 
+0

확실하게, 당신이 입력 스트림을 전달하기 때문에 이것이 믿을만한 작품입니다. – Naresh

0

감사 evilone을

Google에서 문제를 열었습니다. 문제가 Google 지사에서 해결됩니다. 구글 개발자로부터 http://code.google.com/p/android/issues/detail?id=16892

코멘트 :

"나는 우리의 내부 벌집 트리의 루트 문제에 대한 수정 프로그램을 준비했습니다 그러나 당신은 당신의 코드에 대한 수정이 필요하지 않습니다 귀하의 parseXml 방법은 바로해야한다.. String이 아닌 InputStream입니다. InputSource 생성자에 직접 전달할 수 있습니다. "

관련 문제