xml 범용 파싱
@SuppressWarnings("unchecked")
private void getStoreInfo() throws SAXException, Exception {
Properties prop = PropertiesFactory.getProperties(PropertiesPath.ENV.build());
URL url = null;
URLConnection con = null;
for(Map<String, String> paramMap : getPlantList()) {
try {
System.out.println(prop.getProperty("vms.url") + "&plant=" + paramMap.get("PLANT"));
// vms.url=http://xxx.xxx.xxx.xxx:8080/enovia/mobileService/mService.jsp?action=siteInfo
url = new URL(prop.getProperty("vms.url") + "&plant=" + paramMap.get("PLANT"));
con = url.openConnection();
con.connect();
XMLMap xmlMap = new XMLMap(con.getInputStream());
Map<String, Object> resultMap = xmlMap.getMap();
if("200".equals(resultMap.get("code"))) {
// List<Map<String, String>> paramList = (List<Map<String, String>>) resultMap.get("furs");
insertStoreInfo((List<Map<String, String>>) resultMap.get("furs")); // TODO 1개일때는 오류났으나 수정
} else {
System.out.println("CODE : " + resultMap.get("code"));
System.out.println("MESSAGE : " + resultMap.get("msg"));
continue;
}
} catch (IOException e) {
continue;
} catch (SQLException e) {
continue;
}
}
}
@SuppressWarnings("unchecked")
private void xmlStoreInfo() throws SAXException, Exception {
XMLMap xmlMap = new XMLMap(new File("/fashionscm/mobile/furList.xml"));
Map<String, Object> resultMap = xmlMap.getMap();
List<Map<String, String>> paramList = (List<Map<String, String>>) resultMap.get("furs");
insertStoreInfo(paramList);
// for(Map<String, String> storeMap : storeList) {
// System.out.println(storeMap);
// }
}
또다른 사용 예) 안드로이드에서..
@Override
public void update(ResponseEvent response) {
mMyLoadingDialog.dismiss();
String resultText = response.getResultData();
// WriteTextFile("FurInfo.txt", resultText);
try {
InputStream inputstream = new ByteArrayInputStream(resultText.getBytes("UTF-8"));
// 첫번째 방법
// XmlPullFeedParser parser = new XmlPullFeedParser(inputstream);
// List<VMSSiteInfo> messages = parser.parse();
//
// for (VMSSiteInfo vmsSiteInfo : messages) {
// System.out.println(vmsSiteInfo.getPlant()+" "+vmsSiteInfo.getFloor()+" "+
// vmsSiteInfo.getFurCode()+" "+vmsSiteInfo.getFurNo()+" "+vmsSiteInfo.getFurType());
// }
첫번째 방법 링크 : http://androi.tistory.com/204
// 두번째 방법
XMLMap xmlMap = new XMLMap(inputstream);
try {
Map<String, Object> resultMap = xmlMap.getMap();
if ("200".equals(resultMap.get("code"))) {
System.out.println("MESSAGE : " + resultMap.get("msg"));
zoneList = (List<Map<String, String>>) resultMap.get("lists");
for (Map<String, String> map : zoneList) {
System.out.println(map.get("plant") + " " + map.get("floor") + " " + map.get("zone") + " " + map.get("zoneTitle") + " "
+ map.get("zoneX") + " " + map.get("zoneZ") + " " + map.get("zoneSales"));
}
if (zoneList.size() > 0) {
addViews(zoneList.size());
}
} else {
System.out.println("CODE : " + resultMap.get("code"));
System.out.println("MESSAGE : " + resultMap.get("msg"));
}
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
첨부파일
'자바 일반' 카테고리의 다른 글
쿠키 저장 및 조회 from JAVA (1) | 2019.05.28 |
---|---|
Spring 예제 사이트, 게시판 만들기 등 (0) | 2015.11.04 |
ArrayList.newInstance(); HashMap.newInstance(); (0) | 2015.01.09 |
자바 현재 시간 구하기 (0) | 2014.11.20 |
일정 시간에 PC 자동 종료하기와 bat 배치파일 실행시 cmd창 감추기 (0) | 2014.11.20 |