使用jdk自带的api做对象和XML互转

原文链接: https://blog.csdn.net/liliang_11676/article/details/81837215

项目中调用别人的接口,返回的数据是String类型的XML,所以需要XML转成对象,首先贴一下XML样式



    
        {3BDE3D58-96F5-49D8-BA36-CB94097CCE1A}
        company-DefaultControl
        B
        0
    

我们需要根据XML新建对应的对象,接口返回的可能是多个,我这里只贴出来一个先,但是我们建对象的时候需要考虑进来

package pojo;


import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
						
@XmlRootElement(name = "KeyListXml") //表示以KeyListXml作为跟节点来解析
public class KeyListXml {
	
	private List  keyInfo;

	public List getKeyInfo() {
		return keyInfo;
	}

	@XmlElement(name = "KEY_INFO")
	//@XmlElementWrapper(name="KEY_INFO") //这个注解表示KEY_INFO是多个
	public void setKeyInfo(List keyInfo) {
		this.keyInfo = keyInfo;
	}



}
package pojo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "KEY_INFO")
public class KeyInfo {

	private String kcId;
	
	private String keyName;
	
	private String keyKind;
	
	private long keyType;

	public String getKcId() {
		return kcId;
	}

	@XmlElement(name = "kc_id") //这个注解和XML里面的字段对应
	public void setKcId(String kcId) {
		this.kcId = kcId;
	}

	public String getKeyName() {
		return keyName;
	}

	@XmlElement(name = "key_name")
	public void setKeyName(String keyName) {
		this.keyName = keyName;
	}

	public String getKeyKind() {
		return keyKind;
	}

	@XmlElement(name = "key_kind")
	public void setKeyKind(String keyKind) {
		this.keyKind = keyKind;
	}

	public long getKeyType() {
		return keyType;
	}
	
	@XmlElement(name = "key_type")
	public void setKeyType(long keyType) {
		this.keyType = keyType;
	}

	@Override
	public String toString() {
		return "KeyInfo [kcId=" + kcId + ", keyName=" + keyName + ", keyKind=" + keyKind + ", keyType=" + keyType + "]";
	}
	
	

}
 package utils;
    
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import java.io.StringWriter;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    
    /**
     * bean<->xml互转工具类
     * @author tanwg
     * @date 2017年8月30日
     */
    public class XmlHelper {
        public static  T toObj(Class clazz, String xml) {
            try {
                JAXBContext context = JAXBContext
                        .newInstance(new Class[] { clazz });
                InputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
                return (T) context.createUnmarshaller().unmarshal(buf);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static  String toXml(T obj) {
            try {
                StringWriter write = new StringWriter();
                JAXBContext context = JAXBContext.newInstance(new Class[] { obj
                        .getClass() });
                context.createMarshaller().marshal(obj, write);
                return write.getBuffer().toString();
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            return "";
        }
    }

在我们需要解析的地方调用解析方法就可以了

//TODO 解析xml
KeyListXml keyListXml = (KeyListXml)XmlHelper.toObj(KeyListXml.class, varRet.toString());
List infos = keyListXml.getKeyInfo();
if(infos != null && !infos.isEmpty()){
	return infos.get(0);
}

解析出来就是一个KeyInfo对象了,同时我们也可以对象转成 XML,只需要调用XmlHelper .toXml(KerInfo),就可以了

你可能感兴趣的:(java)