붓, 그리다
오라클 DB상에 있는 데이터 XML 문서로 만들기 1 본문
오라클 DB상에 있는 데이터 XML 문서로 만들기
dom을 import
- 최근 거의 사용하지 않는 듯함
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | package db; import java.io.*; import java.util.*; import java.sql.*; import org.dom4j.*; import org.dom4j.io.*; import db.*; public class CreateXMLDemo { //private DBConnectionMgr pool; private MakerDTO maker = new MakerDTO(); private static String jdbcDriver = "oracle.jdbc.driver.OracleDriver"; private static String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; private static String userName = "admin"; private static String userPwd = "1234"; /* // 생성자 : DB 연결 public CreateXMLDemo() { try { pool = DBConnectionMgr.getInstance(); System.out.println("pool > " + pool); } catch (Exception e) { System.out.println("DB 연결 실패 > " + e); } } // 생성자 */ private static Document createDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement("markers"); //MakerDAO dao = new MakerDAO(); ArrayList<MakerDTO> makerList = getMakerList(); try { for(int i=0; i<makerList.size(); i++) { Element superNod = root.addElement("marker"); MakerDTO maker = makerList.get(i); Element element = superNod.addElement("no"); element.addText(Integer.toString(maker.getNo())); element = superNod.addElement("name"); element.addText(maker.getName()); element = superNod.addElement("lat"); element.addText(Double.toString(maker.getLat())); element = superNod.addElement("lng"); element.addText(Double.toString(maker.getLng())); element = superNod.addElement("addr"); element.addText(maker.getAddr()); element = superNod.addElement("locno"); element.addText(maker.getLoctype()); } // for }catch(Exception e) { System.out.println("createDocument()메서드 에러유발" + e); } return document; } // 전체 레코드를 가져오는 메서드 호출 public static ArrayList<MakerDTO> getMakerList() { ArrayList<MakerDTO> makerList = new ArrayList<MakerDTO>(); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { Driver driver = (Driver)Class.forName(jdbcDriver).newInstance(); con = DriverManager.getConnection(jdbcUrl, userName, userPwd); String sql = "select * from marker"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { MakerDTO maker = new MakerDTO(); maker.setNo(rs.getInt("locnum")); maker.setName(rs.getString("locname")); maker.setLat(rs.getDouble("lat")); maker.setLng(rs.getDouble("lng")); maker.setAddr(rs.getString("addr")); maker.setLoctype(rs.getString("loctype")); makerList.add(maker); } } catch (Exception e) { System.out.println("getMakerList()메서드 에러유발" + e); } return makerList; } public static void main(String[] args) { Document document; try { document = createDocument(); OutputFormat format = new OutputFormat("\t", true); FileWriter out = new FileWriter("D:/3.jsp/FinalProject/WebContent/xml/output.xml"); // 저장 원하는 위치 XMLWriter writer = new XMLWriter(out, format); writer.write(document); out.close(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } | cs |
output.xml
'Google Map' 카테고리의 다른 글
XML 문서 내 좌표 값 지도에 표시하기 (0) | 2017.08.29 |
---|---|
오라클 DB상에 있는 데이터 XML 문서로 만들기 2 (0) | 2017.08.29 |
Comments