org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x1c) was found in the CDATA section
지도 API를 사용호출시 브라우저에서 호출하면 정상인데
JAVA 소스에서 호출하면 다음과 같은 에러가 떴다.
(parameter : 서울특별시 강남구 강남대로)
[Fatal Error] :3:22: An invalid XML character (Unicode: 0x1c) was found in the CDATA section.
문제는 브라우저에서 호출할 때, UrlEncoding을 해서 보내는데
그것을 JAVA에서 Decoding을 하고 그대로 사용하니
결국 Encoding안된 parameter가 넘어가서 에러가 나는듯 하네
try{
String address = URLEncoder.encode(addr, "UTF-8");
String apiKey = "Naver 지도API";
String encoding = "utf-8";
String coord = "latlng";//latlng, tm128(navigation 용 : default)
String target = "http://openapi.map.naver.com/api/geocode.php";
String params = "?key=" + apiKey + "&encoding=" + encoding + "&coord=" + coord + "&query=" + address;
URL url = new URL(target+params);
URLConnection connection = url.openConnection();
try{
InputStream in = connection.getInputStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(in);
NodeList itemNodes = doc.getElementsByTagName("item");
for(int i = 0; i < itemNodes.getLength(); i++){
Node item = itemNodes.item(i);
NodeList childNodes = item.getChildNodes();
//public static final short ELEMENT_NODE = 1;
//public static final short ATTRIBUTE_NODE = 2;
//public static final short TEXT_NODE = 3;
for( int j = 0 ; j < childNodes.getLength(); j++){
Element ele = null;
Node childNode = childNodes.item(j);
if (childNode.getNodeType() != Node.ELEMENT_NODE){
continue;
}
String eleTag = childNode.getNodeName();
if( "point".equals(eleTag)){
NodeList pointList = childNode.getChildNodes();
for(int m = 0; m < pointList.getLength(); m++){
Node aPoint = pointList.item(m);
String pointName = aPoint.getNodeName();
if("x".equals(pointName)){
//logger.debug(aPoint.getNodeName() + " : " + aPoint.getTextContent());
}else if("y".equals(pointName)){
//logger.debug(aPoint.getNodeName() + " : " + aPoint.getTextContent());
}
}
break;
}
if( "address".equals(eleTag)){
continue;
}
if( "addrdetail".equals(eleTag)){
continue;
}
}//end of for j
}//end of for i
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}