JAVA工具类之Xml解析

导入jar包

        
        <dependency>
            <groupId>com.thoughtworks.xstreamgroupId>
            <artifactId>xstreamartifactId>
            <version>1.4.7version>
        dependency>

XMLDemo.java

public static void main(String[] args) {
        String dataxml="<xml>"
                + "<return_code>return_code>"
                + "<return_msg>return_msg>"
                + "<result_code>result_code>"
                + "<err_code>err_code>"
                + "<err_code_des>err_code_des>"
                + "<mch_billno>mch_billno>"
                + "<mch_id>mch_id>"
                + "<wxappid>wxappid>"
                + "<re_openid>re_openid>"
                + "<total_amount>100total_amount>"
                + "xml>";
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxRedPackageRsult.class);
        WxRedPackageRsult wxRedPackageRsult=(WxRedPackageRsult) xstream.fromXML(dataxml);
        System.out.println(wxRedPackageRsult.getReturnCode());
    }

实体类
WxRedPackageRsult.java

package com.sc.api.util;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;


/**
 * 
 * 微信推送过来的消息,也是同步回复给用户的消息,xml格式
 * 相关字段的解释看微信开发者文档:
 * http://mp.weixin.qq.com/wiki/17/f298879f8fb29ab98b2f2971d42552fd.html
 * http://mp.weixin.qq.com/wiki/7/9f89d962eba4c5924ed95b513ba69d9b.html

 * 
* * @author antgan */
@XStreamAlias("xml") public class WxRedPackageRsult { /////////////////////// // 以下都是微信推送过来的消息的xml的element所对应的属性 /////////////////////// @XStreamAlias("return_code") @XStreamConverter(value = XStreamCDataConverter.class) private String returnCode; public String getReturnCode() { return returnCode; } public void setReturnCode(String returnCode) { this.returnCode = returnCode; } }

CDATA 转换器
XStreamCDataConverter.java

package com.sc.api.util;

import com.thoughtworks.xstream.converters.basic.StringConverter;

/**
 * CDATA 转换器
 * @author FANQIBU
 *
 */
public class XStreamCDataConverter extends StringConverter {

    @Override
    public String toString(Object obj) {
        return " + super.toString(obj) + "]]>";
    }

}

XStream 自定义初始化器

package com.sc.api.util;

import java.io.Writer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;

/**
 * XStream 自定义初始化器
 * @author FANQIBU
 *
 */
public class XStreamInitializer {

    public static XStream getInstance() {
        XStream xstream = new XStream(new XppDriver() {
            @Override
            public HierarchicalStreamWriter createWriter(Writer out) {
                return new PrettyPrintWriter(out, getNameCoder()) {
                    protected String PREFIX_CDATA = ";
                    protected String SUFFIX_CDATA = "]]>";
                    protected String PREFIX_MEDIA_ID = "";
                    protected String SUFFIX_MEDIA_ID = "";

                    @Override
                    protected void writeText(QuickWriter writer, String text) {
                        if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
                            writer.write(text);
                        } else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
                            writer.write(text);
                        } else {
                            super.writeText(writer, text);
                        }

                    }
                };
            }
        });
        xstream.ignoreUnknownElements();//忽视null的节点
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.addPermission(NullPermission.NULL);
        xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
        return xstream;
    }

}

你可能感兴趣的:(JAVASE)