jquery xml 요청, spring xml 리턴

■ 참고 : [ajax] 기본 샘플 소스 xml 파싱해서 읽어서 뿌리기
 
 
 

■ jquery에서 서버로 xml데이터를 요청하는 예제, dataTye=’xml’로 요청을 한다.

<!DOCTYPE html>
<meta http-equiv="Content-Type"
  content="text/html; charset=utf-8" />
<meta charset="utf-8">
<head>
<script src='/js/jquery/jquery-1.11.0.min.js'></script>
<script>
$(document).ready(function() { 
  $("#log").append("xml download 테스트 시작<br/>");

  $.ajax({
    url       : '/simplexml/content.xml',
    type      : 'post',
    data      : 'name=dummay&age=28',
    dataType  : 'xml',
    success   : function(xml) {
      // xml 문서 item 기준으로 분리후 반복
      $(xml).find('item').each(function(){ 
            var link = $(this).find("link").text();
            var title = $(this).find("title").text();
            var description = $(this).find("description").text();
            var tag = $(this).find("tag").text();

            var view_text = link + title + description + tag ;
            $("#log").append(view_text);  // #id 에 view_text 삽입
        });
    },
    error : function() {
      console.error('error');
    }
  });
});
</script>
</head>
<body><div id="log"></div></body>
</html>

 
 
 

■ spring controller 코드, produces=”application/xml;charset=utf-8″

@Controller
@RequestMapping("simplexml")
public class SimpleXMLController {

  @RequestMapping(
      value   = "content.xml",
      method = RequestMethod.POST,
      produces="application/xml;charset=utf-8")
  @ResponseBody
  public String  listSample(HttpServletRequest request)
      throws Exception {

    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("<item>");
    stringBuffer.append("  <category>");
    stringBuffer.append("    <![CDATA[ 카테고리 ]]>");
    stringBuffer.append("  </category>");
    stringBuffer.append("  <title>");
    stringBuffer.append("    <![CDATA[ 제목 ]]>");
    stringBuffer.append("  </title>");
    stringBuffer.append("  <link>");
    stringBuffer.append("    http://주소..");
    stringBuffer.append("  </link>");
    stringBuffer.append("  <description>");
    stringBuffer.append("    <![CDATA[ 내용 ]]>");
    stringBuffer.append("  </description>");
    stringBuffer.append("  <tag>");
    stringBuffer.append("    <![CDATA[ 태그 ]]>");
    stringBuffer.append("  </tag>");
    stringBuffer.append("</item>");

    return stringBuffer.toString();
  }
}

 
 
 

■ 크롬 디버거에서 본 xml 리턴 모습
크롬 디버거에서 본 xml 리턴 모습