백엔드기술/개발언어
JAXB - XML 스키마 자동으로 Class로 변환
RevFactory
2011. 12. 23. 17:03
1. trang.jar 다운로드
2. XML -> XSD
java -jar trang.jar test.xml test.xsd
3. XSD -> JAVA
xjc test.xsd
*xjc 는 [자바폴더]\binf 에 있음
4. 패키지 변경
폴더를 적당한 위치로 변경하고, 각 Java파일의 패키지명을 알맞게 수정
5. 아래 유틸 소스로 객체화
매개변수 : XML주소, 패키지명
public class JAXBParser {
public static Object loadXMLData(String urlStr, String packageName) throws IOException {
URL url = null;
URLConnection con = null;
Object root = null;
try {
JAXBContext context = JAXBContext.newInstance(packageName);
Unmarshaller unmarshaller = context.createUnmarshaller();
url = new URL(urlStr);
//System.out.println(url);
con = url.openConnection();
root = unmarshaller.unmarshal(con.getInputStream());
} catch(MalformedURLException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
return root;
}
}
2. XML -> XSD
java -jar trang.jar test.xml test.xsd
3. XSD -> JAVA
xjc test.xsd
*xjc 는 [자바폴더]\binf 에 있음
4. 패키지 변경
폴더를 적당한 위치로 변경하고, 각 Java파일의 패키지명을 알맞게 수정
5. 아래 유틸 소스로 객체화
매개변수 : XML주소, 패키지명
public class JAXBParser {
public static Object loadXMLData(String urlStr, String packageName) throws IOException {
URL url = null;
URLConnection con = null;
Object root = null;
try {
JAXBContext context = JAXBContext.newInstance(packageName);
Unmarshaller unmarshaller = context.createUnmarshaller();
url = new URL(urlStr);
//System.out.println(url);
con = url.openConnection();
root = unmarshaller.unmarshal(con.getInputStream());
} catch(MalformedURLException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
return root;
}
}