간단한 unmarshal 예제
개발/JAVA2011. 10. 26. 22:19
728x90
public static Config getConfigData(String path){ Config element = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(Config.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); element = (Config) unmarshaller.unmarshal (new InputStreamReader(new FileInputStream(path),"UTF-8")); System.out.println (element.getId() ); System.out.println (element.getName() ); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return element; }
path는 xml경로를 지정하면 된다.
@XmlRootElement class Config { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
XML은 다음과 같은 형식을 가진다.
1 일퍼센트
728x90
댓글 영역