본문 바로가기
Programming/JavaScript

ajax xml 파싱

by 막이 2014. 11. 11.

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
function resultAjax() {
    var url = "/api.do?cmd=contentsList";
    var params =  $("form[name=frm]").serialize();
        $.ajax({      
            type:"POST",  
            url:url,      
            data:params,      
            dataType: "xml",
            success: function(xml) {
                xmlParsing(xml);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);    
                console.log(textStatus);    
                console.log(errorThrown);    
             }
        });  
}
 
 
 
 
function xmlParsing(text){
 
    var resultCode = text.getElementsByTagName("resultCode")[0].innerHTML;
 
    //초기화
    $('#result').text('');
 
    if(resultCode=="0"){
    
        //xml 데이터로 테이블 만들기
        xmlParser(text);
        
    }else{
        //div result 에 error 라고 뿌려준다 
        $('#result').text("error");
    }
        
}
 
 
function xmlParser(xml) {
     
    var content='';
 
    $(xml).find("list").each(function () {
 
        var videoidx = $(this).find("videoidx").text(); 
        var title = $(this).find("title").text();
        var imgUrl = $(this).find("imgUrl").text();
        var star = $(this).find("star").text(); 
        var rCnt = $(this).find("rCnt").text(); 
        var ncsNm = $(this).find("ncsNm").text();  
        var openlecturer = $(this).find("openlecturer").text(); 
 
        content = content + 
         '<table border="1" width="100%" class="lecture">'+
                        '<colgroup>'+
                        '<col width="16%">'+
                        '<col width="16%">'+
                        '<col width="16%">'+
                        '<col width="50%">'+
                    '</colgroup>'+
            '<tr>'+
                '<td colspan="3"><img src="'+imgUrl+'" width="192" height="110" alt="no image"></td>'+
                '<td rowspan=5"></td>'+
            '</tr>'+
            '<tr>'+
                '<td colspan="3" class="bold">'+title+'</td>'+
            '</tr>'+
            '<tr>'+
                '<td class="bold">가격</td>'+
                '<td class="bold">평점</td>'+
                '<td class="bold">수강</td>'+
            '</tr>'+
            '<tr>'+
                '<td>무료</td>'+
                '<td><img src="/images/ocs/btn/icon_star5.gif" alt="5점"></td>'+
                '<td>167</td>'+
            '</tr>'+
            '<tr>'+
                '<td colspan="3"></td>'+
            '</tr>'+
            '</table>';
 
 
    });
 
    $("#result").append(content);
 
}

완성된 코드는 아니지만 대충 이렇게 뿌려줬음..

첨엔 소스에 아래 처럼 getElementByTagName()[0].innerHTML; 식으로 가져왔는데

<![CDATA["+chapterVO.getObjinfo()+"]]> 감싸준 CDATA 부분까지 다 가져와서 으헝 T_T  

5시간 삽질함 

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
function xmlParsing(text){
 
    var resultCode = text.getElementsByTagName("resultCode")[0].innerHTML;
 
    //초기화
    $('#result').text('');
 
    if(resultCode=="0"){
 
        xmlParser(text);
 
        /*
        var nodes = text.getElementsByTagName("list");
 
        for (i=0;i<nodes.length; i++)
        {
            var node = nodes[i];
            var videoidx = node.getElementsByTagName("videoidx")[0].innerHTML;
            var title = node.getElementsByTagName("title")[0].innerHTML;
            var imgUrl = node.getElementsByTagName("imgUrl")[0].innerHTML;
            var star = node.getElementsByTagName("star")[0].innerHTML;
            var rCnt = node.getElementsByTagName("rCnt")[0].innerHTML;
            var ncsNm = node.getElementsByTagName("ncsNm")[0].innerHTML;
            var openlecturer = node.getElementsByTagName("openlecturer")[0].innerHTML;
 
            content = content + 
                     '<table border="1" width="100%" class="lecture">'+
                                    '<colgroup>'+
                                    '<col width="16%">'+
                                    '<col width="16%">'+
                                    '<col width="16%">'+
                                    '<col width="50%">'+
                                '</colgroup>'+
                        '<tr>'+
                            '<td colspan="3"><img src="'+imgUrl+'" width="192" height="110" alt="no image"></td>'+
                            '<td rowspan=5"></td>'+
                        '</tr>'+
                        '<tr>'+
                            '<td colspan="3" class="bold">'+title+'</td>'+
                        '</tr>'+
                        '<tr>'+
                            '<td class="bold">가격</td>'+
                            '<td class="bold">평점</td>'+
                            '<td class="bold">수강</td>'+
                        '</tr>'+
                        '<tr>'+
                            '<td>무료</td>'+
                            '<td><img src="/images/ocs/btn/icon_star5.gif" alt="5점"></td>'+
                            '<td>167</td>'+
                        '</tr>'+
                        '<tr>'+
                            '<td colspan="3"></td>'+
                        '</tr>'+
                        '</table>';
        }
 
        */
        //console.log(content);
 
        //$('#result').html(content);
            
    }else{
        $('#result').text("error");
    }
    
    
}




---------------------------------------------------------------------------- 

필요없을 수도 있지만 java단

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
     * 강의 리스트
     * 
     * @param req
     * @param res
     * @return
     * @throws IOException 
     * @throws Exception
     */
    public void contentsList(HttpServletRequest  request, HttpServletResponse response) throws Exception {
        
        int resultCode = SUCCESS;
        
        int rowsPerPage = 0;
        int currentPage =  0;
        String srchType =  "";
        String orderType = "";
        
        String[] ncsCd = {};
        String supNcsCd = "";
        String srchCate  = "";
        String majorClCd = "";
        String srchCofRslt = "";
        String openOrg  = "";
        String pageDiv  = "";
        String schTitle  = "";
        String mp4Type  = "";
        
        StringBuffer sb = new StringBuffer();
        sb.append("    <response>");
        
        try{
            
            rowsPerPage = ServletRequestUtils.getIntParameter(request, "rowsPerPage", 15);
            currentPage = ServletRequestUtils.getIntParameter(request, "currentPage", 1);
            srchType = ServletRequestUtils.getStringParameter(request, "srchType""");
            orderType = ServletRequestUtils.getStringParameter(request, "orderType""new");
            /*
             * orderType : new- 신규 / star - 추천 / rCnt- 인기 
             */
            
            ncsCd = ServletRequestUtils.getStringParameters(request, "ncsCd");
            supNcsCd = ServletRequestUtils.getStringParameter(request, "supNcsCd");
            srchCate = ServletRequestUtils.getStringParameter(request, "srchCate""subject");
            majorClCd = ServletRequestUtils.getStringParameter(request, "majorClCd""1");            // 좌측 메뉴 중분류 전공계열구분코드
            srchCofRslt = ServletRequestUtils.getStringParameter(request, "srchCofRslt");
            openOrg = ServletRequestUtils.getStringParameter(request, "openOrg");
//            pageDiv = ServletRequestUtils.getStringParameter(request, "pageDiv", "");
            schTitle = ServletRequestUtils.getStringParameter(request, "schTitle""");
            mp4Type = ServletRequestUtils.getStringParameter(request, "mp4Type""");
 
            
        }catch (Exception e) {
            resultCode = PARAMETER_INVALID;
        }
        
        
        if(resultCode==SUCCESS){
            
            try{
                    //PageNavi
                    PageNavigator pageNavi = makePageNavigator(request);
                    pageNavi.setRowsPerPage(rowsPerPage);
                    pageNavi.setCurrentPage(currentPage);
        
                    
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("schTitle", schTitle);
                    map.put("pageNavi", pageNavi);
                    map.put("srchType", srchType);
                    map.put("orderType", orderType);
                    map.put("ncsCd", ncsCd);
                    map.put("srchCate", srchCate);
                    map.put("majorClCd", majorClCd);
                    map.put("openOrg", openOrg);
                    map.put("srchCofRslt", srchCofRslt);
                    map.put("mp4Type", mp4Type);
                    map.put("supNcsCd", supNcsCd);
                    
                    String srchOpencrsClCd = "";
                    
                    if (srchType.equals("01")) {//이러닝
                        srchOpencrsClCd = "30";
                    } else if(srchType.equals("04")) {//국내외
                        srchOpencrsClCd = "inOut";
                    } else if(srchType.equals("05")) {//직업방송
                        srchOpencrsClCd = "40";
                    }
                    
                    map.put("srchOpencrsClCd", srchOpencrsClCd);
                    
                    String srchTypeOld = "";
                    if (srchType.equals("04")) {
                        srchTypeOld = "04";
                        map.put("srchType""01");
                    } else if(srchType.equals("05")) {
                        srchTypeOld = "05";
                        map.put("srchType""01");
                    } else {
                        map.put("srchType", srchType);
                    }
                    
                    
                    ResultList videoList = jukeboxService.selectVideoList(map);
                    pageNavi.setTotalSize(videoList.getTotalSize());
                    
        
                    JukeboxCVO  jukebox = new JukeboxCVO();
                    Chapter chapterVO  = new Chapter(); 
                    
                    sb.append("    <desc>Search alive Open API</desc>");
                    sb.append("    <pageno>"+currentPage+"</pageno>");
                    
                    
                        forint i =0 ; i < videoList.getSize() ; i++  ){
                            jukebox = (JukeboxCVO)videoList.get(i);
                            
                            //강좌목록
                            Map<String, Object> chapterMap = new HashMap<String, Object>();
                            chapterMap.put("chcontent", jukebox.getVideoIdx());
                            ResultList chapterList = elearningOpenLectureService.selectElearningChapterList(chapterMap);
                            
                            
                            sb.append("<list>");
                            sb.append("    <videoidx>"+jukebox.getVideoIdx()+"</videoidx>");
                            sb.append("    <title><![CDATA["+jukebox.getTitle()+"]]></title>"); //강의명
                            sb.append("    <url><![CDATA["+jukebox.getUrl()+"]]></url>");
                            String imgUrl = "";
                            if(jukebox.getImgFileId()!=null && jukebox.getImgFileId() !="0"){
                                imgUrl="http://bizhrd.net/ocs/contents.do?cmd=downloadImgFile&fileid="+jukebox.getImgFileId();
                            }
                            sb.append("    <imgUrl><![CDATA["+imgUrl+"]]></imgUrl>");
                            sb.append("    <videoFrm>"+jukebox.getMp4()+"</videoFrm>"); //동영상 확장자  01 :플래시   02: 동영상
                            sb.append("    <star>"+jukebox.getStar()+"</star>"); //평점
                            sb.append("    <rCnt>"+jukebox.getrCnt()+"</rCnt>"); //수강인원
                            sb.append("    <ncsNm>><![CDATA["+jukebox.getNcsNm()+"]]></ncsNm>");  //주제분류
                            //sb.append("    <cdName>"+jukebox.getCdName()+"</cdName>"); 
                            sb.append("    <source>"+jukebox.getSource()+"</source>"); //제공기관
                            sb.append("    <openyy>"+jukebox.getOpenyy()+"</openyy>"); //제작년도
                            sb.append("    <openlecturer>"+jukebox.getOpenlecturer()+"</openlecturer>"); //강의자 
                            sb.append("    <openguide><![CDATA["+jukebox.getOpenguide()+"]]></openguide>");  //강의설명
                            sb.append("    <regDate>"+jukebox.getRegdate()+"</regDate>"); //등록일
                            sb.append("    <chapterCnt>"+chapterList.getSize()+"</chapterCnt>"); //등록일
                            
                            if(chapterList.getSize() > 0){
                                forint j =0 ; j < chapterList.getSize() ; j++  ){
                                    chapterVO = (Chapter)chapterList.get(j);
                                    sb.append("<chapterlist>");
                                    sb.append("<chapId>"+chapterVO.getChapid()+"</chapId>");
                                    sb.append("<objinfo><![CDATA["+chapterVO.getObjinfo()+"]]></objinfo>");
                                    sb.append("<url><![CDATA["+chapterVO.getUrl()+"]]></url>");
                                    sb.append("<sortNo>"+chapterVO.getSortno()+"</sortNo>");
                                    sb.append("<videoFrm>"+chapterVO.getOpencrsVideoFrm()+"</videoFrm>");
                                    sb.append("</chapterlist>");
                                }
                                
                            }
                            
                            
                            sb.append("</list>");
                        }
                        
                    ///ocs/contents.do?cmd=downloadImgFile&fileid=
                    
                    //OPENCRS_CL_CD 공개강의 구분코드 10 또는 20 이면  cdName / majorNm
            
            }catch(Exception e){
                resultCode = PROCESS_ERROR;
                logger.error(e);
            }
        
        }
        
        sb.append("    <resultCode>"+resultCode+"</resultCode>");
        sb.append("    </response>");
        
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/xml");
        response.getWriter().print(sb.toString());
    }

XML

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?xml version="1.0" encoding="utf-8"?>
<response>
<desc>Search alive Open API</desc>
<pageno>1</pageno>
<list>
<videoidx>100000000000013890</videoidx>
<title>
<![CDATA[ 캐릭터,디자인 종사자를 위한 저작권 노하우 ]]>
</title>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/01/swf/index.html
]]>
</url>
<imgUrl>
<![CDATA[
http://bizhrd.net/ocs/contents.do?cmd=downloadImgFile&fileid=26143
]]>
</imgUrl>
<videoFrm>01</videoFrm>
<star>3</star>
<rCnt>640</rCnt>
<ncsNm>
>
<![CDATA[ 법률·경찰·소방·교도·국방 > 법률 교육·자연·사회과학 > 평생교육 ]]>
</ncsNm>
<source>한국산업인력공단</source>
<openyy>null</openyy>
<openlecturer>null</openlecturer>
<openguide>
<![CDATA[ null ]]>
</openguide>
<regDate>2014-04-10 00:00:00.0</regDate>
<chapterCnt>6</chapterCnt>
<chapterlist>
<chapId>18924</chapId>
<objinfo>
<![CDATA[ 지식재산에 의한 캐릭터의 법적 보호 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/01/swf/index.html
]]>
</url>
<sortNo>1</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18925</chapId>
<objinfo>
<![CDATA[ 캐릭터 저작물성과 저작권법에 의한 보호 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/02/swf/index.html
]]>
</url>
<sortNo>2</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18926</chapId>
<objinfo>
<![CDATA[ 캐릭터의 창작과 법적 쟁점 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/03/swf/index.html
]]>
</url>
<sortNo>3</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18927</chapId>
<objinfo>
<![CDATA[ 캐릭터 상품화를 위한 추가적인 법적 보호장치 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/04/swf/index.html
]]>
</url>
<sortNo>4</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18928</chapId>
<objinfo>
<![CDATA[ 캐릭터의 상표등록과 권리의 조정 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/05/swf/index.html
]]>
</url>
<sortNo>5</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18929</chapId>
<objinfo>
<![CDATA[ 캐릭터 라이선싱 프로세스 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/403/06/swf/index.html
]]>
</url>
<sortNo>6</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
</list>
<list>
<videoidx>100000000000013891</videoidx>
<title>
<![CDATA[ 콘텐츠 수출산업 종사자를 위한 저작권 노하우 ]]>
</title>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/01/swf/index.html
]]>
</url>
<imgUrl>
<![CDATA[
http://bizhrd.net/ocs/contents.do?cmd=downloadImgFile&fileid=26144
]]>
</imgUrl>
<videoFrm>01</videoFrm>
<star>0</star>
<rCnt>656</rCnt>
<ncsNm>
>
<![CDATA[ 교육·자연·사회과학 > 평생교육 법률·경찰·소방·교도·국방 > 법률 ]]>
</ncsNm>
<source>한국산업인력공단</source>
<openyy>null</openyy>
<openlecturer>null</openlecturer>
<openguide>
<![CDATA[ null ]]>
</openguide>
<regDate>2014-04-10 00:00:00.0</regDate>
<chapterCnt>6</chapterCnt>
<chapterlist>
<chapId>18930</chapId>
<objinfo>
<![CDATA[ 글로벌화하는 중국법 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/01/swf/index.html
]]>
</url>
<sortNo>1</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18931</chapId>
<objinfo>
<![CDATA[ 콘텐츠 비즈니스 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/02/swf/index.html
]]>
</url>
<sortNo>2</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18932</chapId>
<objinfo>
<![CDATA[ 중국 저작권법 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/03/swf/index.html
]]>
</url>
<sortNo>3</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18933</chapId>
<objinfo>
<![CDATA[ 저작권 라이선스 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/04/swf/index.html
]]>
</url>
<sortNo>4</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18934</chapId>
<objinfo>
<![CDATA[ 라이선스 작성 실무 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/05/swf/index.html
]]>
</url>
<sortNo>5</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
<chapterlist>
<chapId>18935</chapId>
<objinfo>
<![CDATA[ 권리침해에 대한 구제 ]]>
</objinfo>
<url>
<![CDATA[
http://portal.hrd.go.kr/EL/contents/404/06/swf/index.html
]]>
</url>
<sortNo>6</sortNo>
<videoFrm>01</videoFrm>
</chapterlist>
</list>
<resultCode>0</resultCode>
</response>