ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JSP] 영화 xml데이터 가져오기
    Web/JSP 2019. 10. 12. 11:40

    <%@page import="java.text.DecimalFormat"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="org.w3c.dom.*"%>
    <%@ page import="javax.xml.parsers.*" %>
    <%@ page import="java.util.*" %>    
        
    <%
    //map을 담을 리스트 만들기
    List<Map> pubList = new ArrayList();
    
    //xml 데이터 호출하기
    String url = "http://192.168.2.254:18888/Day07/07_day_movieList.xml";
    
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    
    DocumentBuilder builder = factory.newDocumentBuilder();
    
    Document document = builder.parse(url);
    
    NodeList items = document.getElementsByTagName("dailyBoxOffice");
    
    String[] elements = {"rank", "rankOldAndNew", "movieNm", "openDt", "salesAcc", "audiAcc"};
    
    for(int i=0; i < items.getLength(); i++) {
    	Node n = items.item(i);
    	
    	Element e = (Element) n;
    	
    	Map<String, String> pub = new HashMap();
    	
    	//child를 기준으로 for문 만들기
    	for(String element : elements) {
    		NodeList titleList = e.getElementsByTagName(element);
    		Element titleElement = (Element)titleList.item(0);
    		Node titleNode = titleElement.getChildNodes().item(0);
    		pub.put(element, titleNode.getNodeValue());
    	}
    	//리스트에 map 담기
    	pubList.add(pub);
    }
    
    %>  
    
    <%!
    //쉼표 추가를 위한 메소드 선언
    DecimalFormat df = new DecimalFormat("#,##0");
    public String myFormat(String str) {
    	return df.format( Long.parseLong(str) );
    }
    %>  
        
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <meta charset="UTF-8">
    <title>movie_xml_parse.jsp</title>
    </head>
    <body>
    <div class="container">
    	<table class="table table-dark table-striped">
    	 <thead>
    		<tr >
    			<th>순위</th>
    			<th>영화명</th>
    			<th>오픈일</th>
    			<th>누적관객수</th>
    			<th>누적수익</th>
    			<th>신규랭킹진입여부</th>
    		</tr>
    	</thead>
    	<tbody>
    		<%for(Map pub : pubList){ %>
    		<tr>
    			<td><%=pub.get("rank") %></td>
    			<td onclick="buyTicket('<%=pub.get("movieNm") %>')"><%=pub.get("movieNm") %></td>
    			<td><%=pub.get("openDt") %></td>
    			<td><%=myFormat(String.valueOf(pub.get("audiAcc"))) %>명</td>
    			<td><%=myFormat(String.valueOf(pub.get("salesAcc"))) %>원</td>
    			<td bgcolor="<%="NEW".equals(pub.get("rankOldAndNew"))?"#ffff44":"#0000ff"%>">
    				<%=pub.get("rankOldAndNew") %>
    			</td>
    		</tr>
    		<%} %>	
    	<tbody>	
    	</table>
    	
    	<script>
    	function buyTicket(a){
    		
    		alert(a+'티켓 구매하러 가기');
    	}
    	</script>
    	</div>
    </body>
    </html>
    

     

    반응형

    댓글

Designed by Tistory.