java中实体类转换成xml格式

package cn.ac.sec.service.empi.guizhou;

import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.rpc.Service;

import cn.ac.sec.common.constant.Constant;
import cn.ac.sec.common.constant.URLConstant;
import cn.ac.sec.common.util.PropertiesUtil;
import cn.ac.sec.model.app.AppUser;
import cn.ac.sec.service.empi.guizhou.model.PersonInfo;
import cn.com.mandalat.UHIDService.UHIDServiceLocator;
import cn.com.mandalat.UHIDService.UHIDServiceSoapStub;

public class UHIDService {
	protected static String GZ_EMPI_WS_URL=null;
	static{
	PropertiesUtil util = new PropertiesUtil(Constant.CUSTOM_PROPERTIES);
	GZ_EMPI_WS_URL=util.readKey(Constant.GZ_EMPI_WS_URL);
	}
	
	public static String entrty2XML(PersonInfo pserson) throws Exception{
		
		        JAXBContext context = JAXBContext.newInstance(PersonInfo.class);
		        Marshaller marshaller = context.createMarshaller();
		        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		        marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
		        StringWriter writer = new StringWriter();
		        marshaller.marshal(pserson, writer);
		        System.out.println(writer.toString());
		        String str= writer.toString();
		        int i = str.indexOf("")+"".length() ;
		        int j = str.indexOf("");
		        System.out.println( str.substring(i, j));
		        return str.substring(i, j);
	}
	public static PersonInfo getPersonInfo(AppUser user){
		PersonInfo personInfo = new PersonInfo();
		personInfo.setUID(null);
		personInfo.setName(user.getRealname());
		personInfo.setGender(user.getGender().toString());
		personInfo.setHoroscopy(user.getBirthday());
		personInfo.setTerritory(user.getAddress());
		personInfo.setIdiom(null);
		personInfo.setMater(null);
		personInfo.setMemory(null);
		personInfo.setCertType(user.getType());
		personInfo.setCertId(user.getIdNumber());
		return personInfo;
	}
	/**
	 * 注册
	 */
	public static void UHIDregister(AppUser user){
		PersonInfo personInfo = getPersonInfo(user);
		String requesthead = "";
		String requestfoot = "";
		try {
			String str = entrty2XML(personInfo);
			String requestStr = requesthead + str + requestfoot;
			URL endpointURL =new URL(GZ_EMPI_WS_URL);
			Service service = (Service) new UHIDServiceLocator();
			UHIDServiceSoapStub stub = new UHIDServiceSoapStub(endpointURL, service);
			stub.EMPIExecute(requestStr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 修改
	 */
    public static void UHIDupdate(){
		
	}
    /**
     * 注销
     */
    public static void UHIDcancle(){
    	
    }
    /**
     * 查询
     */
    public static void UHIDquery(){
    	
    }
	public static void main(String[] args) {
		AppUser app = new AppUser();
		app.setAddress("beijing");
		app.setRealname("wangn");
		app.setIdNumber("123456780987654");
		app.setType("sfz");
		app.setGender(1);
		app.setBirthday("20180509");
		PersonInfo p = getPersonInfo(app);

		try {
		entrty2XML(p);	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

输出为:



    wangning
    1
    20180509
    beijing
    sfz
    123456780987654



    wangning
    1
    20180509
    beijing
    sfz
    123456780987654

另外注意实体类中的字段需要使用

@XmlRootElement进行标注生成xml的节点

实体类:

import java.util.Arrays;

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

/**
 * 贵州人员实体类
 * @author fdwdz
 *
 */
@XmlAccessorType(value=XmlAccessType.FIELD) 
@XmlRootElement
public class PersonInfo {
	/**
	 * 主索引UID
	 */
	@XmlElement
	private String UID;
	/**
	 * 姓名、称呼
	 */
	@XmlElement
	private String Name;
	/**
	 * 性别
	 */
	@XmlElement
	private String Gender;
	/**
	 * 出生日期
	 */
	@XmlElement
	private String Horoscopy;
	/**
	 * 出生地省级行政区
	 */
	@XmlElement
	private String Territory;
	/**
	 * 出生地语言标识
	 */
	@XmlElement
	private String Idiom;
	/**
	 * 母亲姓名
	 */
	@XmlElement
	private String Mater;
	/**
	 * 联系人姓名
	 */
	@XmlElement
	private String Memory;
	/**
	 * 身份证件数组
	 */
	@XmlElement
	private String[] Certifications;
	/**
	 * 证件类型
	 */
	@XmlElement
	private String CertType;
	/**
	 * 证件号
	 */
	@XmlElement
	private String CertId;

你可能感兴趣的:(java)