2009-10-06 4 views
0

질문 제목이 복잡해 보일지라도이 문제는 매우 간단합니다. 나는 다음과 같은 코드를 사용하여이 파일을 구문 분석 할 때 다음Groovy 빌더를 사용하여 UTF-8로 XML 파일을 변환하는 방법 StreamingMarkupBuilder

def xmlFile = new File("file-${System.currentTimeMillis()}.xml") 
mb = new groovy.xml.StreamingMarkupBuilder() 
mb.encoding = "UTF-8" 
new FileWriter(xmlFile) << mb.bind { 
    mkp.xmlDeclaration() 
    out << "\n" 
    someMarkup {} 
} 

:

나는 다음 스크립트와 XML 파일을 만들

def xml = new XmlSlurper().parse(xmlFile) 

나는 다음과 같은 MalformedByteSequenceException 예외를 가지고 :

예외 발생 : 잘못된 바이트 2가 3 바이트 UTF-8 시퀀스

그리고 UTF-8 형식으로 파일을 변환하면 (예 : 메모장 ++ 사용) 모든 것이 정상입니다.

그래서 파일을 UTF-8 형식으로 저장하려면 어떻게해야합니까? 왜 코드 mb.encoding = "UTF-8"은 그것을하지 않습니까?

들으

당신은 FileOutputStream에 주위 출력 스트림 라이터를 포장 할 필요가
+0

이 블로그를 참조 할 수 있습니다. http://mrhaki.blogspot.com/2009/10/groovy-goodness-creating-xml-with.html 도움이 될 것입니다 –

답변

2

는 UTF-8은 아마, 내가 mb.encoding가 무엇을 설정 모르겠어요 기본 캐릭터 세트

new OutputStreamWriter(new FileOutputStream(exportXmlFile),'utf-8') << mb.bind { 
    mkp.xmlDeclaration() 
    out << "\n" 
    someMarkup {} 
} 

가 없습니다 xml 헤더에 charset을 설정합니다.

+0

Thx. 잘 작동한다. – fabien7474