java-XML报文的拼装----工具类生成XML报文

XML编码模型类(一):

package com.XML.test;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class XMLEncodeModel {

	private Map heads = new HashMap();//XML报文头
	private Map roots = new HashMap();//XML报文数据

	/**往XML模型添加报文头
	 * @param key 数据名
	 * @param value 数据值
	 * @return
	 */
	public String setHeadParameter(String key,String value){
		if(heads == null){
			heads = new HashMap();
		}
		return heads.put(key, value);
	}
	
	/**往XML模型添加报文数据
	 * @param key 数据名
	 * @param value 数据值
	 * @return
	 */
	public String setRootParameter(String key,String value){
		if(roots == null){
			roots = new HashMap();
		}
		return roots.put(key, value);
	}

	public String getHeadParameter(String key){
		return heads!=null?heads.get(key):null;
	}
	public String getRootParameter(String key){
		return roots!=null?roots.get(key):null;
	}

	
	/**  产生模型对应的XML数据
	 * @param charset 编码
	 * @return
	 */
	public String toSendData(Charset charset){
		StringBuilder builder = new StringBuilder();
		builder.append("");
		builder.append("");
		builder.append("");
		if(heads!=null){
			for(Entry keyVal:heads.entrySet()){
				if(keyVal!=null){
					builder.append("<").append(keyVal.getKey()).append(">");
					builder.append(keyVal.getValue()!=null?keyVal.getValue():"");
					builder.append("");
				}
			}
		}
		builder.append("");
		builder.append("");
		if(roots!=null){
			for(Entry keyVal:roots.entrySet()){
				if(keyVal!=null){
					builder.append("<").append(keyVal.getKey()).append(">");
					builder.append(keyVal.getValue()!=null?keyVal.getValue():"");
					builder.append("");
				}
			}
		}
		builder.append("");
		builder.append("");
		return builder.toString();
	}
}

测试:

	public static void main(String[] args) {
		XMLEncodeModel model = new XMLEncodeModel();
		model.setHeadParameter("HeadName1", "value1");
		model.setHeadParameter("HeadName2", "value2");
		model.setHeadParameter("HeadName3", "value3");
		
		model.setRootParameter("RootName1", "RootValue1");
		model.setRootParameter("RootName2", "RootValue2");
		model.setRootParameter("RootName3", "RootValue3");
		
		String xmlString = model.toSendData(Charset.forName("GBK"));
		System.out.println(xmlString);
	}

结果:

value3value2value1RootValue3RootValue2RootValue1

           整理后:



	
		value3
		value2
		value1
	
	
		RootValue3
		RootValue2
		RootValue1
	

XML文件格式可以根据不同的接口要求对toSendData()方法进行修改

 

也可以把xml报文头和报文数据单独的写成模型类:

XML编码模型类(二):

          报文头模型:

public class XMLHeadmodel {

	private String HeadName1;
	
	private String HeadName2;

	private String HeadName3;

	
	public XMLHeadmodel() {
		super();
	}

	public XMLHeadmodel(String headName1, String headName2, String headName3) {
		super();
		HeadName1 = headName1;
		HeadName2 = headName2;
		HeadName3 = headName3;
	}

	public String getHeadName1() {
		return HeadName1;
	}

	public void setHeadName1(String headName1) {
		HeadName1 = headName1;
	}

	public String getHeadName2() {
		return HeadName2;
	}

	public void setHeadName2(String headName2) {
		HeadName2 = headName2;
	}

	public String getHeadName3() {
		return HeadName3;
	}

	public void setHeadName3(String headName3) {
		HeadName3 = headName3;
	}

	
}

           报文数据模型:

public class XMLRootmodel {

	private String RootName1;
	
	private String RootName2;

	private String RootName3;

	
	public XMLRootmodel() {
		super();
	}

	public XMLRootmodel(String rootName1, String rootName2, String rootName3) {
		super();
		RootName1 = rootName1;
		RootName2 = rootName2;
		RootName3 = rootName3;
	}

	public String getRootName1() {
		return RootName1;
	}

	public void setRootName1(String RootName1) {
		RootName1 = RootName1;
	}

	public String getRootName2() {
		return RootName2;
	}

	public void setRootName2(String RootName2) {
		RootName2 = RootName2;
	}

	public String getRootName3() {
		return RootName3;
	}

	public void setRootName3(String RootName3) {
		RootName3 = RootName3;
	}

	
}

          xml模型类:

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class XMLEncodeModel2 {

	private XMLHeadmodel heads = null;//XML报文头
	private XMLRootmodel roots = null;//XML报文数据

	public void setHeads(XMLHeadmodel heads) {
		this.heads = heads;
	}
	
	public void setRoots(XMLRootmodel roots) {
		this.roots = roots;
	}
	
	public XMLHeadmodel getHeads() {
		return heads;
	}

	public XMLRootmodel getRoots() {
		return roots;
	}

	/**  产生模型对应的XML数据
	 * @param charset 编码
	 * @return
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public String toSendData(Charset charset) throws IllegalArgumentException, IllegalAccessException{
		StringBuilder builder = new StringBuilder();
		builder.append("");
		builder.append("");
		builder.append("");
		Class headsclz = heads.getClass();
		Field[] headsfields = headsclz.getDeclaredFields();
		for (Field field : headsfields) {
			field.setAccessible(true);
			if(field.get(heads)!=null){
				builder.append("<").append(field.getName()).append(">");
				builder.append((String) field.get(heads));
				builder.append("");
			}else {
				System.out.println("XML报文头字段:"+ field.getName() + " 为空");
			}
		}
		builder.append("");
		builder.append("");
		Class rootsclz = roots.getClass();
		Field[] rootsfields = rootsclz.getDeclaredFields();
		for (Field field : rootsfields) {
			field.setAccessible(true);
			if(field.get(roots)!=null){
				builder.append("<").append(field.getName()).append(">");
				builder.append((String) field.get(roots));
				builder.append("");
			}else {
				System.out.println("XML报文数据字段:"+ field.getName() + " 为空");
			}
		}
		builder.append("");
		builder.append("");
		return builder.toString();
	}
}

测试:

	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
		XMLEncodeModel2 model = new XMLEncodeModel2();
		XMLHeadmodel heads = new XMLHeadmodel("headvalue1","headvalue2","headvalue3");//报文头
		XMLRootmodel roots = new XMLRootmodel("rootvalue1","rootvalue2","rootvalue3");//报文数据
		model.setHeads(heads);
		model.setRoots(roots);
		String xmlString = model.toSendData(Charset.forName("GBK"));
		System.out.println(xmlString);
	}

结果整理后和上面第一个测试一样



	
		headvalue1
		headvalue2
		headvalue3
	
	
		rootvalue1
		rootvalue2
		rootvalue3
	

 

你可能感兴趣的:(java-util)