ITeye博客

XStream 转化XML时候不能支持CDATA标签

xining · 2013-06-19 13:22 · XML文件解析 · Web前端 · Markdown

XStream 转化XML时候不能支持CDATA标签导致部分字符被转义。

import java.io.IOException; import java.io.Writer;

import org.apache.commons.lang.StringUtils;

import com.thoughtworks.xstream.core.util.FastStack; import com.thoughtworks.xstream.core.util.QuickWriter; import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class MyPrettyPrintWriter implements ExtendedHierarchicalStreamWriter {

private final QuickWriter writer;
private final FastStack elementStack = new FastStack(16);
private final char lineIndenter;

private boolean tagInProgress;
private int depth;
private boolean readyForNewLine;
private boolean tagIsEmpty;
private boolean breakLine = false; // 是否换行

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 = "&quot;".toCharArray();
private static final char APOS = "&apos;".toCharArray();
private static final char CLOSE = "</".toCharArray();

public TpaicPrettyPrintWriter(Writer writer, char lineIndenter, boolean breakLine, boolean needXMLHead, String encoding) throws IOException {
    this.writer = new QuickWriter(writer);
    this.lineIndenter = lineIndenter;
    this.breakLine = breakLine;

    // 增加XML头
    String xmlHeader = "<?xml version=\"1.0\" encoding=\"" + (StringUtils.isNotBlank(encoding) ? encoding : "GBK") + "\"?>";
    if (needXMLHead && breakLine) {
        writer.write(xmlHeader + "\n");
    }

    if (needXMLHead && !breakLine) {
        writer.write(xmlHeader);
    }
}

public TpaicPrettyPrintWriter(Writer writer, boolean breakLne, boolean needXMLHead, String encoding) throws IOException {
    // this(writer, new char { ' ', ' ' }); // 节点之间以空格分隔
    this(writer, breakLne ? new char { ' ', ' ' } : new char {}, breakLne, needXMLHead, encoding);
}

public TpaicPrettyPrintWriter(Writer writer, boolean breakLne) throws IOException {
    // this(writer, new char { ' ', ' ' }); // 节点之间以空格分隔
    this(writer, breakLne ? new char { ' ', ' ' } : new char {}, breakLne, true, "GBK");
}

public void startNode(String name) {
    tagIsEmpty = false;
    finishTag();
    writer.write('<');
    writer.write(name);
    elementStack.push(name);
    tagInProgress = true;
    depth++;
    readyForNewLine = true;
    tagIsEmpty = true;
}

@SuppressWarnings("unchecked")
public void startNode(String name, Class clazz) {
    startNode(name);
}

public void setValue(String text) {
    readyForNewLine = false;
    tagIsEmpty = false;
    finishTag();

    writeText(writer, text);
}

public void addAttribute(String key, String value) {
    writer.write(' ');
    writer.write(key);
    writer.write('=');
    writer.write('"');
    writeAttributue(writer, value);
    writer.write('"');
}

protected void writeAttributue(QuickWriter writer, String text) {
    int length = text.length();
    for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        switch (c) {
            case '&':
                this.writer.write(AMP);
                break;
            case '<':
                this.writer.write(LT);
                break;
            case '>':
                this.writer.write(GT);
                break;
            case '"':
                this.writer.write(QUOT);
                break;
            case '\'':
                this.writer.write(APOS);
                break;
            case '\r':
                this.writer.write(SLASH_R);
                break;
            default:
                this.writer.write(c);
        }
    }
}

protected void writeText(QuickWriter writer, String text) {
    int length = text.length();
    String CDATAPrefix = "<!


import java.io.IOException;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

public class MyXppDriver extends XppDriver {
    public static final Log logger = LogFactory.getLog(TpaicXppDriver.class);

    private boolean breakLine = false; // 是否换行

    private boolean needXmlHeader = true;

    private String encoding = "GBK"; // xml头编码方式

    public TpaicXppDriver(boolean breakLine, boolean needXmlHeader, String encoding) {
        this.breakLine = breakLine;
        this.needXmlHeader = needXmlHeader;
        this.encoding = encoding;
    }

    public TpaicXppDriver() {
        this(false, true, "GBK");
    }

    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
        HierarchicalStreamWriter writer = null;
        try {
            writer = new MyPrettyPrintWriter(out, breakLine, needXmlHeader, encoding);
        } catch (IOException e) {
            logger.error(e);
        }
        return writer;
    }
}



    public static String objectToXmlSupportCDATA(Object object, boolean format, boolean needXmlHead, String encoding) {
        XStream xstream = new XStream(new MyXppDriver(format, needXmlHead, encoding));
        xstream.autodetectAnnotations(true);
        return xstream.toXML(object);
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。