달력

52024  이전 다음

  • 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

'xstream'에 해당되는 글 2건

  1. 2008.08.17 [XStream] 몇가지 메모
  2. 2008.05.08 [xstream] CDATA 추가하기.
1. java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamException 에러 발생시
대개 JDK 1.5를 사용하는 경우에 발생한다. XMLStreamException클래스는 JDK 6.0 부터 추가된 클래스이다. JDK 1.5를 사용한다면 stax-api-1.0.1.jar 파일을 클래스패스에 추가하면 된다.

2. JSON 문자열을 반환하고자 할때
JSON 문자열을 반환하기 위해 사용가능한 드라이버는 JsonHierarchicalStreamDriver와 JettisonMappedXmlDriver 두가지가 있다. JSON문자열은 기본적으로 한줄에 모두 표기가 되어야 하기 때문에 특별한 경우가 아니라면 JettisonMappedXmlDriver 를 사용해야 한다. JsonHierarchicalStreamDriver는 XML과 같은 구조형태로 JSON문자열을 변환한다.
이 경우 추가해야 할 jar파일은 jettison-1.0-RC2.jar 이다.
Posted by fromm0
|
import java.io.Writer;
import java.util.regex.Pattern;

import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;

public class MyPrettyPrintWriter extends PrettyPrintWriter {
    private static final char[] AMP = "&".toCharArray();
    private static final char[] LT = "<".toCharArray();
    private static final char[] GT = ">".toCharArray();
    private static final char[] SLASH_R = " ".toCharArray();
    private static final char[] QUOT = """.toCharArray();
    private static final char[] APOS = "'".toCharArray();
   
    public LocalPrettyPrintWriter(Writer writer) {
        super(writer);
    }

    protected void writeText(QuickWriter writer, String text) {       
       
        String CDATAPrefix = "<![CDATA[";
        String CDATASuffix = "]]>";

        if (!text.startsWith(CDATAPrefix) && !Pattern.matches("[^[0-9]]+", text)) {
            text = CDATAPrefix+text+CDATASuffix;
        }
       
        int length = text.length();
        if (!text.startsWith(CDATAPrefix)) {
            for (int i = 0; i < length; i++) {
                char c = text.charAt(i);
                switch (c) {
                case '&':
                    writer.write(AMP);
                    break;
                case '<':
                    writer.write(LT);
                    break;
                case '>':
                    writer.write(GT);
                    break;
                case '"':
                    writer.write(QUOT);
                    break;
                case '\'':
                    writer.write(APOS);
                    break;
                case '\r':
                    writer.write(SLASH_R);
                    break;
                default:
                    writer.write(c);
                }
            }
        } else {
            for (int i = 0; i < length; i++) {
                char c = text.charAt(i);
                writer.write(c);
            }
        }
    }
}


import java.io.Writer;

import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class MyXppDriver extends XppDriver {

    public HierarchicalStreamWriter createWriter(Writer out) {
        return new MyPrettyPrintWriter(out);
    }
}





Posted by fromm0
|