jdom2를 이용한 spring xml response(응답)

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

■ pom.xml에 jdom2를 추가한다.

<dependency>
  <groupId>org.jdom</groupId>
  <artifactId>jdom2</artifactId>
  <version>2.0.6</version>
</dependency>

 
 
 

■ 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″

import javax.servlet.http.HttpServletRequest;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@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 {

    final Document doc     = new Document();
    final Element item     = new Element("item");

    final Element category     = new Element("category");
    category.setAttribute("Name","P");
    category.setAttribute("age", "23");
    category.setText("text");

    final Element title     = new Element("title");
    title.setText("제목");

    item.addContent(category);
    item.addContent(title);

    doc.addContent(item);

    return new XMLOutputter().outputString(doc);
}
}

 
 
 

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

 
 
 

■ pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.iotinfra</groupId>
<artifactId>pilot.simplexml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <java-version>1.7</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<org.springframework-version>4.2.5.RELEASE</org.springframework-version>

<!-- Test -->
<junit.version>4.11</junit.version>
<mockito-all.version>1.10.19</mockito-all.version>

<SERVER_MODE>local</SERVER_MODE>

</properties>

<dependencies>
    <!-- Spring -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6</version>
</dependency>

</dependencies>

<build>
<finalName>${project.artifactId}</finalName>

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.5</version>
    </plugin>

    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <port>9904</port>
            <path>/</path>
            <contextReloadable>true</contextReloadable>
            <systemProperties>
                <JAVA_OPTS>-Xms512m -Xmx512m --XX:MaxPermSize=128m</JAVA_OPTS>
            </systemProperties>
        </configuration>
    </plugin>
</plugins>

</build>
</project>

 
 
 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다