본문 바로가기

프로그래밍/Java

XML 파싱 시 유니코드 문자 포함 오류 발생 (0xb, 0x1, 0x1b, 0x3 등)

반응형

DocumentBuilder.parse(...)로 XML 파싱 중 오류 발생

[Fatal Error] :1:428: An invalid XML character (Unicode: 0xb) was found in the element content of the document.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 428; An invalid XML character (Unicode: 0xb) was found in the element content of the document.
	at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
	at com.egovframework.test.SimpleTest.main(SimpleTest.java:51)


콘텐츠에 다음과 같은 유니코드 문자가 들어가 있었으나, 에디터에서 '나눔고딕코딩' 폰트 문제로 유니코드가 보이지 않아서 삽질하였음.


0xb: : Vertical tabulation

0x1b:  : Escape

0x3: : End of text

0x1: : Start of heading


유니코드 문자열 검색은 아래 홈페이지에서

https://unicode-table.com




결론

유니코드 문자열을 정규식으로 삭제 처리


line = line.replaceAll("[^\\u0009\\u000A\\u000D\\u0020-\\uD7FF\\uE000-\\uFFFD\\u10000-\\u10FFF]+", "");


반응형

개발자가 그리는 인스타툰 팔로우하세요!