반응형

* 필요 jar File

XStreamReq&xml2json.zip


TestMain.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
import com.exam.domain.AuthKey;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
 
import com.exam.domain.AuthKey;
import com.exam.util.xstream.Obj2Xml;
 
public class TestMain {
    public static void main(String[] args) throws JSONException{
        AuthKey authKey = new AuthKey();
        
        authKey.setPrimaryKey("key");
        authKey.setId("a001122334455");
        authKey.setProperty_1("pro1");
        authKey.setProperty_2("pro2");
        String xml = new Obj2Xml(authKey).toXml();
        System.out.println("===== XML OUT =====");
        System.out.println(xml);
        
        JSONObject jsonObj = XML.toJSONObject(xml);
        System.out.println("===== JSON OUT =====");
        System.out.println(jsonObj);
    }
}
cs

AuthKey.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
package com.exam.domain;
 
public class AuthKey {
    private String primaryKey;
    private String id;
    private String property_1;
    private String property_2;
    public String getPrimaryKey() {
        return primaryKey;
    }
    public void setPrimaryKey(String primaryKey) {
        this.primaryKey = primaryKey;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getProperty_1() {
        return property_1;
    }
    public void setProperty_1(String property_1) {
        this.property_1 = property_1;
    }
    public String getProperty_2() {
        return property_2;
    }
    public void setProperty_2(String property_2) {
        this.property_2 = property_2;
    }
    @Override
    public String toString() {
        return "AuthKey [primaryKey=" + primaryKey + ", id=" + id
                + ", property_1=" + property_1 + ", property_2=" + property_2
                + "]";
    }
        
}
cs

Obj2Xml.java(Obj를 받아서 Xml(String)으로 변환)

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
package com.exam.util.xstream;
 
import com.exam.domain.AuthKey;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
 
public class Obj2Xml {
    private Object obj;
    
    public Obj2Xml(Object obj){
        this.obj = obj;
    }
    
    public String toXml(){
        String toXml;
        
        XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("__","_");
        XStream xStream = new XStream(new DomDriver("UTF-8", replacer)){
            protected boolean useXStreamXmlFriendlyMapper() {
                return true;
            }
        };
        xStream.alias("AuthKey", AuthKey.class);
        toXml = xStream.toXML(obj);
        return toXml;
    }
    
}
cs

Result

1
2
3
4
5
6
7
8
9
===== XML OUT =====
 
  key
  a001122334455
  pro1
  pro2
 
===== JSON OUT =====
{"response":{"id":"a001122334455","property_1":"pro1","primaryKey":"key","property_2":"pro2"}}
cs

*****************************************************

*****************************************************

Xstream 을 사용할때 Obj 속성중 _ 가 있으면 __ 로 변환 되기 때문에 아래처럼 XmlStream을 선언하여 써야 한다.

참고

1
2
3
4
5
6
XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("__","_");
        XStream xStream = new XStream(new DomDriver("UTF-8", replacer)){
            protected boolean useXStreamXmlFriendlyMapper() {
                return true;
            }
        };
cs


반응형

'IT > 언어' 카테고리의 다른 글

페이징 처리용 소스(예제?)  (0) 2014.07.08
POI Excel 파일 쓰기  (0) 2014.06.26
Mybatis + java 연동 기초  (0) 2014.06.19
JDBC 설정  (0) 2014.06.19
[MyBatis] SqlSessionFactory  (0) 2014.06.19

+ Recent posts