结合具体数据上传业务实现一个XML生成框架
当自己的系统需要向别的数据平台系统上传数据时,可能规则是由数据平台定的。
下面就是一个WEB Service业务接口的典型定义。
班级信息上传接口
自己系统的业务表设计(关系型数据库):
年级信息
package com.zas.data.xml;/** * 年级信息 * @author zas * */public class Grade {//年级IDString gradeID;//年级编码String gradeCode;//年级名称String gradeName;//年级描述String gradeDes;public Grade() {super();}public Grade(String gradeID, String gradeCode, String gradeName,String gradeDes) {super();this.gradeID = gradeID;this.gradeCode = gradeCode;this.gradeName = gradeName;this.gradeDes = gradeDes;}public String getGradeID() {return gradeID;}public void setGradeID(String gradeID) {this.gradeID = gradeID;}public String getGradeName() {return gradeName;}public void setGradeName(String gradeName) {this.gradeName = gradeName;}public String getGradeDes() {return gradeDes;}public void setGradeDes(String gradeDes) {this.gradeDes = gradeDes;}public String getGradeCode() {return gradeCode;}public void setGradeCode(String gradeCode) {this.gradeCode = gradeCode;}@Overridepublic String toString() {return "Grade [gradeID=" + gradeID + ", gradeCode=" + gradeCode+ ", gradeName=" + gradeName + ", gradeDes=" + gradeDes + "]";}/** * @param args */public static void main(String[] args) {}}
班级
package com.zas.data.xml;/** * 班级信息 * @author zas * */public class ClassInformation{ //班级IDString classID;//班级编号String classCode;//班级名称String className;//年级IDString gradeID;//班级描述String classDes;public ClassInformation() {super();}public ClassInformation(String classID, String classCode, String className,String gradeID, String classDes) {super();this.classID = classID;this.classCode = classCode;this.className = className;this.gradeID = gradeID;this.classDes = classDes;}public String getClassID() {return classID;}public void setClassID(String classID) {this.classID = classID;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getGradeID() {return gradeID;}public void setGradeID(String gradeID) {this.gradeID = gradeID;}public String getClassDes() {return classDes;}public void setClassDes(String classDes) {this.classDes = classDes;}public String getClassCode() {return classCode;}public void setClassCode(String classCode) {this.classCode = classCode;}@Overridepublic String toString() {return "ClassInformation [classID=" + classID + ", classCode="+ classCode + ", className=" + className + ", gradeID="+ gradeID + ", classDes=" + classDes + "]";}}
package com.zas.data.xml;import java.util.Date;/** * 学生数据对象 * @author zas * */public class Student {//学生IDString id;//学号String code;//年级String gradeid;//班级String classid;//姓名String name;//性别String sexCode;//生日Date birthDay;public Student() {super();}public Student(String id, String code, String gradeid, String classid,String name, String sexCode, Date birthDay) {super();this.id = id;this.code = code;this.gradeid = gradeid;this.classid = classid;this.name = name;this.sexCode = sexCode;this.birthDay = birthDay;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getGradeid() {return gradeid;}public void setGradeid(String gradeid) {this.gradeid = gradeid;}public String getClassid() {return classid;}public void setClassid(String classid) {this.classid = classid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSexCode() {return sexCode;}public void setSexCode(String sexCode) {this.sexCode = sexCode;}public Date getBirthDay() {return birthDay;}public void setBirthDay(Date birthDay) {this.birthDay = birthDay;}@Overridepublic String toString() {return "Student [id=" + id + ", code=" + code + ", gradeid=" + gradeid+ ", classid=" + classid + ", name=" + name + ", sexCode="+ sexCode + ", birthDay=" + birthDay + "]";}/** * @param args */public static void main(String[] args) {}}
package com.zas.data.xml;import java.util.List;import java.util.Map;/** * xml结构图 * @author zas * */public interface DataMapForXml {/** * 参数元数据map * 主要存储当前层信息 * @return */Map<String, List<ParameterMetaData>> getParameterMap();/** * 当有一对多关系时填入其中 * @return */Map<String, List<DataMapForXml>> getOneToManyList();}
package com.zas.data.xml;/** * 元数据类 * @author zas * */public class ParameterMetaData {//标识String parameterIdentifier = null;//名称String parameterName = null;//Java类型String parameterType = null;//值Object parameterValue = null;//描述String parameterDescription = null;//前缀String elementPrefix = null;//类型String parameterClientType = null;//是否必填Boolean isRequired = false;//规格String parameterSpecification = null;public ParameterMetaData(String parameterIdentifier, String parameterName,String parameterType, Object parameterValue,String parameterDescription, String elementPrefix,String parameterPhilipsType, Boolean isRequired,String parameterSpecification) {super();this.parameterIdentifier = parameterIdentifier;this.parameterName = parameterName;this.parameterType = parameterType;this.parameterValue = parameterValue;this.parameterDescription = parameterDescription;this.elementPrefix = elementPrefix;this.parameterClientType = parameterPhilipsType;this.isRequired = isRequired;this.parameterSpecification = parameterSpecification;}public String getParameterName() {return parameterName;}public void setParameterName(String parameterName) {this.parameterName = parameterName;}public String getParameterType() {return parameterType;}public void setParameterType(String parameterType) {this.parameterType = parameterType;}public Object getParameterValue() {return parameterValue;}public void setParameterValue(Object parameterValue) {this.parameterValue = parameterValue;}public String getParameterDescription() {return parameterDescription;}public void setParameterDescription(String parameterDescription) {this.parameterDescription = parameterDescription;}public String getElementPrefix() {return elementPrefix;}public void setElementPrefix(String elementPrefix) {this.elementPrefix = elementPrefix;}public String getParameterClientType() {return parameterClientType;}public void setParameterClientType(String parameterClientType) {this.parameterClientType = parameterClientType;}public String getParameterSpecification() {return parameterSpecification;}public void setParameterSpecification(String parameterSpecification) {this.parameterSpecification = parameterSpecification;}public String getParameterIdentifier() {return parameterIdentifier;}public void setParameterIdentifier(String parameterIdentifier) {this.parameterIdentifier = parameterIdentifier;}public Boolean getIsRequired() {return isRequired;}public void setIsRequired(Boolean isRequired) {this.isRequired = isRequired;}}
package com.zas.data.xml;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;/** * 班级信息上传 * @author zas * */public class CLASS_INFO implements DataMapForXml {/** 标识名 名称 类型 长度 是否必填 说明 CLASS_INFO CODE 班级编码 String 20 是 班级编码 NAME 班级名称 String 20 是 班级名称 GRADE 年级信息 String 20 是 1、2、3 COUNT 班级人数 Number 8 是 班级人数 CONTENT 简要介绍 String 120 否 班级简要介绍 STUDENT_INFO CODE 学号 String 20 是 学生编码 NAME 姓名 String 20 是 学生姓名 SEX 性别 String 1 否 1:男 0:女 BIRTHDAY 生日 String 20 否 yyyy-mm-dd *///班级编码String CLASS_INFO_CODE;//班级名称String CLASS_INFO_NAME;//年级信息String CLASS_INFO_GRADE;//班级人数Integer CLASS_INFO_COUNT;//班级介绍String CLASS_INFO_CONTENT;List<DataMapForXml> studentList;@Overridepublic Map<String, List<ParameterMetaData>> getParameterMap() {//先后顺序很重要Map<String, List<ParameterMetaData>> elementMap = new LinkedHashMap<String, List<ParameterMetaData>>();List<ParameterMetaData> listCLASS_INFO = new ArrayList<ParameterMetaData>();listCLASS_INFO.add(new ParameterMetaData("CODE","班级编码", "String", this.CLASS_INFO_CODE, "班级编码", "CLASS_INFO", "String", true, ""));listCLASS_INFO.add(new ParameterMetaData("NAME","班级名称", "String", this.CLASS_INFO_NAME, "班级名称", "CLASS_INFO", "String", true, ""));listCLASS_INFO.add(new ParameterMetaData("GRADE","年级信息", "String", this.CLASS_INFO_GRADE, "年级信息", "CLASS_INFO", "String", true, ""));listCLASS_INFO.add(new ParameterMetaData("COUNT","班级人数", "Integer", this.CLASS_INFO_COUNT, "班级人数", "CLASS_INFO", "Number", true, ""));listCLASS_INFO.add(new ParameterMetaData("CONTENT","班级介绍", "String", this.CLASS_INFO_CONTENT, "班级介绍", "CLASS_INFO", "String", false, ""));elementMap.put("CLASS_INFO", listCLASS_INFO);return elementMap;}@Overridepublic Map<String, List<DataMapForXml>> getOneToManyList() {//先后顺序很重要Map<String, List<DataMapForXml>> elementMap = new LinkedHashMap<String, List<DataMapForXml>>();elementMap.put("STUDENT_INFOS", studentList);return elementMap;}public String getCLASS_INFO_CODE() {return CLASS_INFO_CODE;}public void setCLASS_INFO_CODE(String cLASS_INFO_CODE) {CLASS_INFO_CODE = cLASS_INFO_CODE;}public String getCLASS_INFO_NAME() {return CLASS_INFO_NAME;}public void setCLASS_INFO_NAME(String cLASS_INFO_NAME) {CLASS_INFO_NAME = cLASS_INFO_NAME;}public String getCLASS_INFO_GRADE() {return CLASS_INFO_GRADE;}public void setCLASS_INFO_GRADE(String cLASS_INFO_GRADE) {CLASS_INFO_GRADE = cLASS_INFO_GRADE;}public Integer getCLASS_INFO_COUNT() {return CLASS_INFO_COUNT;}public void setCLASS_INFO_COUNT(Integer cLASS_INFO_COUNT) {CLASS_INFO_COUNT = cLASS_INFO_COUNT;}public String getCLASS_INFO_CONTENT() {return CLASS_INFO_CONTENT;}public void setCLASS_INFO_CONTENT(String cLASS_INFO_CONTENT) {CLASS_INFO_CONTENT = cLASS_INFO_CONTENT;}public List<DataMapForXml> getStudentList() {return studentList;}public void setStudentList(List<DataMapForXml> studentList) {this.studentList = studentList;}static class STUDENT_INFO implements DataMapForXml {//学号String STUDENT_INFO_CODE;//姓名String STUDENT_INFO_NAME;//性别String STUDENT_INFO_SEX;//生日String STUDENT_INFO_BIRTHDAY;@Overridepublic Map<String, List<ParameterMetaData>> getParameterMap() {Map<String, List<ParameterMetaData>> elementMap = new LinkedHashMap<String, List<ParameterMetaData>>();List<ParameterMetaData> listSTUDENT_INFO = new ArrayList<ParameterMetaData>();listSTUDENT_INFO.add(new ParameterMetaData("CODE","学号", "String", this.STUDENT_INFO_CODE, "", "STUDENT_INFO", "String", true, ""));listSTUDENT_INFO.add(new ParameterMetaData("NAME","姓名", "String", this.STUDENT_INFO_NAME, "", "STUDENT_INFO", "String", true, ""));listSTUDENT_INFO.add(new ParameterMetaData("SEX","性别", "String", this.STUDENT_INFO_SEX, "", "STUDENT_INFO", "String", false, ""));listSTUDENT_INFO.add(new ParameterMetaData("BIRTHDAY","生日", "Integer", this.STUDENT_INFO_BIRTHDAY, "", "STUDENT_INFO", "String", false, ""));elementMap.put("STUDENT_INFO", listSTUDENT_INFO);return elementMap;}@Overridepublic Map<String, List<DataMapForXml>> getOneToManyList() {return null;}public String getSTUDENT_INFO_CODE() {return STUDENT_INFO_CODE;}public void setSTUDENT_INFO_CODE(String sTUDENT_INFO_CODE) {STUDENT_INFO_CODE = sTUDENT_INFO_CODE;}public String getSTUDENT_INFO_NAME() {return STUDENT_INFO_NAME;}public void setSTUDENT_INFO_NAME(String sTUDENT_INFO_NAME) {STUDENT_INFO_NAME = sTUDENT_INFO_NAME;}public String getSTUDENT_INFO_SEX() {return STUDENT_INFO_SEX;}public void setSTUDENT_INFO_SEX(String sTUDENT_INFO_SEX) {STUDENT_INFO_SEX = sTUDENT_INFO_SEX;}public String getSTUDENT_INFO_BIRTHDAY() {return STUDENT_INFO_BIRTHDAY;}public void setSTUDENT_INFO_BIRTHDAY(String sTUDENT_INFO_BIRTHDAY) {STUDENT_INFO_BIRTHDAY = sTUDENT_INFO_BIRTHDAY;}}}
package com.zas.data.xml;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.XMLWriter;public class BuildXML {/** * 根据数据xml结构图生成xml文件 * @param elementList * @param rootName 暂定为交易号的根名称 * @return */public static String buildXML(List<DataMapForXml> elementList, String rootName) {Document document = DocumentHelper.createDocument();Element root = document.addElement(rootName);createRootElement(elementList, root);try {XMLWriter output = new XMLWriter(new FileWriter(new File("F:/catalog/catalog.xml")));output.write(document);output.close();} catch (IOException e) {System.out.println(e.getMessage());}return "F:/catalog/catalog.xml";}/** * 创建xml文件内容 * @param elementList * @param e */private static void createRootElement(List<DataMapForXml> elementList, Element e) {if(null == elementList || elementList.size() < 1){return;}for (DataMapForXml dataMapForXml : elementList) {//首先处理当前层信息Map<String, List<ParameterMetaData>> elementMap = dataMapForXml.getParameterMap();if(null != elementMap && elementMap.size() > 0){for (String str : elementMap.keySet()) {Element element = e.addElement(str);for (ParameterMetaData pmd : elementMap.get(str)) {if(pmd.getParameterValue() != null){Element elementInner = element.addElement(pmd.getParameterIdentifier());elementInner.setText(pmd.getParameterValue().toString());}}}}//再处理一对多信息Map<String, List<DataMapForXml>> mapList = dataMapForXml.getOneToManyList();if(null != mapList && mapList.size() > 0){for (String str : mapList.keySet()) {Element element = e.addElement(str);//递归处理一对多关系所有的内容createRootElement(mapList.get(str), element);}}}}/** * @param args */public static void main(String[] args) {List<DataMapForXml> elementList = new ArrayList<DataMapForXml>();CLASS_INFO classInfo = new CLASS_INFO();classInfo.setCLASS_INFO_CODE("10001");classInfo.setCLASS_INFO_NAME("一班");classInfo.setCLASS_INFO_GRADE("一年级");classInfo.setCLASS_INFO_CONTENT("一年级一班");classInfo.setCLASS_INFO_COUNT(8);List<DataMapForXml> list = new ArrayList<DataMapForXml>();for (int i = 0; i < 8; i++) {CLASS_INFO.STUDENT_INFO student = new CLASS_INFO.STUDENT_INFO();student.setSTUDENT_INFO_CODE("10001_"+i);student.setSTUDENT_INFO_NAME("学生_" + i);if(i % 2 == 0){student.setSTUDENT_INFO_SEX("男");}else{student.setSTUDENT_INFO_BIRTHDAY("1998-0" + i + "-0" + i);}list.add(student);}classInfo.setStudentList(list);elementList.add(classInfo);BuildXML.buildXML(elementList, "root");}}