1.xml檔案,下面是我要解析的xml檔案
<?xml version="1.0" encoding="utf-8"?>
<config>
<!-- 生成分類 -->
<category>
<category value="curd" label="增删改查(單表)">
<template>curd/controller.xml</template>
<template>curd/restController.xml</template>
<template>curd/service.xml</template>
<template>category-ref:dao</template>
<template>curd/viewForm.xml</template>
<template>curd/viewList.xml</template>
</category>
<category value="curd_many" label="增删改查(一對多)">
<template>curd/controller.xml</template>
<template>curd/restController.xml</template>
<template>curd/serviceMany.xml</template>
<template>category-ref:dao</template>
<template>curd/viewFormMany.xml</template>
<template>curd/viewList.xml</template>
<childTable>
<template>category-ref:dao</template>
</childTable>
</category>
<category value="dao" label="僅持久層(dao/entity/mapper)">
<template>dao/dao.xml</template>
<template>dao/entity.xml</template>
<template>dao/mapper.xml</template>
</category>
<category value="treeTable" label="樹結構表(一體)">
<template>treetable/controller.xml</template>
<template>treetable/restController.xml</template>
<template>treetable/service.xml</template>
<template>treetable/dao.xml</template>
<template>treetable/entity.xml</template>
<template>treetable/mapper.xml</template>
<template>treetable/viewForm.xml</template>
<template>treetable/viewList.xml</template>
</category>
<category value="treeTableAndList" label="樹結構表(左樹右表)">
<template>category-ref:dao</template>
</category>
</category>
<!-- java類型 -->
<javaType>
<dict value="String" label="String"/>
<dict value="Long" label="Long"/>
<dict value="Integer" label="Integer"/>
<dict value="Double" label="Double"/>
<dict value="java.util.Date" label="Date"/>
<dict value="com.m2plat.his.modules.sys.entity.User" label="User"/>
<dict value="com.m2plat.his.modules.sys.entity.Office" label="Office"/>
<dict value="com.m2plat.his.modules.sys.entity.Area" label="Area"/>
<dict value="This" label="ThisObj" description="生成目前對象"/>
<dict value="Custom" label="Custom" description="自定義對象,生成後手動設定"/>
</javaType>
<!-- 查詢類型 -->
<queryType>
<dict value="=" label="="/>
<dict value="!=" label="!="/>
<dict value=">" label=">"/>
<dict value=">=" label=">="/>
<dict value="<" label="<"/>
<dict value="<=" label="<="/>
<dict value="between" label="Between"/>
<dict value="like" label="Like"/>
<dict value="left_like" label="Left Like"/>
<dict value="right_like" label="Right Like"/>
</queryType>
<!-- 字段顯示類型 -->
<showType>
<dict value="input" label="單行文本"/>
<dict value="textarea" label="多行文本"/>
<dict value="select" label="下拉選項"/>
<dict value="radiobox" label="單選按鈕"/>
<dict value="checkbox" label="複選框"/>
<dict value="dateselect" label="日期選擇"/>
<dict value="userselect" label="人員選擇"/>
<dict value="officeselect" label="部門選擇"/>
<dict value="areaselect" label="區域選擇"/>
<dict value="treeselect" label="樹選擇控件"/>
<dict value="fileselect" label="檔案上傳選擇"/>
</showType>
</config>
2.然後建一個解析的對象類
@XmlRootElement(name="config")
public class GenConfig implements Serializable {
private static final long serialVersionUID = L;
private List<GenCategory> categoryList; // 代碼模闆分類
private List<Dict> javaTypeList; // Java類型
private List<Dict> queryTypeList; // 查詢類型
private List<Dict> showTypeList; // 顯示類型
public GenConfig() {
super();
}
@XmlElementWrapper(name = "category")
@XmlElement(name = "category")
public List<GenCategory> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<GenCategory> categoryList) {
this.categoryList = categoryList;
}
@XmlElementWrapper(name = "javaType")
@XmlElement(name = "dict")
public List<Dict> getJavaTypeList() {
return javaTypeList;
}
public void setJavaTypeList(List<Dict> javaTypeList) {
this.javaTypeList = javaTypeList;
}
@XmlElementWrapper(name = "queryType")
@XmlElement(name = "dict")
public List<Dict> getQueryTypeList() {
return queryTypeList;
}
public void setQueryTypeList(List<Dict> queryTypeList) {
this.queryTypeList = queryTypeList;
}
@XmlElementWrapper(name = "showType")
@XmlElement(name = "dict")
public List<Dict> getShowTypeList() {
return showTypeList;
}
public void setShowTypeList(List<Dict> showTypeList) {
this.showTypeList = showTypeList;
}
}
其中GenCategory 類
@XmlRootElement(name="category")
public class GenCategory extends Dict {
private static final long serialVersionUID = L;
private List<String> template; // 主表模闆
private List<String> childTableTemplate;// 子表模闆
public static String CATEGORY_REF = "category-ref:";
public GenCategory() {
super();
}
@XmlElement(name = "template")
public List<String> getTemplate() {
return template;
}
public void setTemplate(List<String> template) {
this.template = template;
}
@XmlElementWrapper(name = "childTable")
@XmlElement(name = "template")
public List<String> getChildTableTemplate() {
return childTableTemplate;
}
public void setChildTableTemplate(List<String> childTableTemplate) {
this.childTableTemplate = childTableTemplate;
}
}
Dict類
public class Dict extends DataEntity<Dict> {
private static final long serialVersionUID = L;
private String value; // 資料值
private String label; // 标簽名
private String type; // 類型
private String description;// 描述
private Integer sort; // 排序
private String parentId;//父Id
public Dict() {
super();
this.isNewRecord=true;//ID采用自增長
}
public Dict(String id){
super(id);
this.isNewRecord=true;//ID采用自增長
}
public Dict(String value, String label){
this.value = value;
this.label = label;
this.isNewRecord=true;//ID采用自增長
}
@XmlAttribute
@Length(min=, max=)
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@XmlAttribute
@Length(min=, max=)
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
@Length(min=, max=)
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@XmlAttribute
@Length(min=, max=)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@NotNull
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
@Length(min=, max=)
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
@Override
public String toString() {
return label;
}
}
其中@XmlAttribute:該屬性作為xml的attribute
XML轉化成對象
public static <T> T fileToObject(String fileName, Class<?> clazz){
try {
String pathName = "/templates/modules/gen/" + fileName;
logger.debug("File to object: {}", pathName);
Resource resource = new ClassPathResource(pathName);
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line == null){
break;
}
sb.append(line).append("\r\n");
}
if (is != null) {
is.close();
}
if (br != null) {
br.close();
}
return (T) JaxbMapper.fromXml(sb.toString(), clazz);
} catch (IOException e) {
logger.warn("Error file convert: {}", e.getMessage(),e);
}
return null;
}
其中JaxbMapper.fromXml(sb.toString(), clazz)代碼
public static <T> T fromXml(String xml, Class<T> clazz) {
try {
StringReader reader = new StringReader(xml);
return (T) createUnmarshaller(clazz).unmarshal(reader);
} catch (JAXBException e) {
throw Exceptions.unchecked(e);
}
}
調用方法
public static GenConfig getConfig(){
return fileToObject("config.xml", GenConfig.class);
}