复合类型封装:
package org.tbyf;
/**
* 复合类型封装
*
* @author 杨虹昌
*/
public class ElementNode {
private String name;// 元素名
private String type="xs:string";// 类型名
public ElementNode(String name) {
this.name = name;
}
public ElementNode() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ElementNode(String name, String type) {
super();
this.name = name;
this.type = type;
}
}
XSD生成类封装:
package org.tbyf;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
* xsd 生成
* @author YHC
*/
public class XSDGenerate {
//文档对象
private Document document;
private Element header;
private Element body;
private Element request;
private Element root;
/**
* 初始化XSD
*/
public XSDGenerate() {
document= DocumentHelper.createDocument();
root=initRootElement();
request=createRequest();
}
/**
* 初始化头部信息
* @return
*/
private Element initRootElement(){
Element root = document.addElement("xs:schema");
root.addNamespace("xs", "http://www.w3.org/2001/XMLSchema");
root.addAttribute("elementFormDefault", "qualified");
root.addAttribute("attributeFormDefault", "unqualified");
return root;
}
/**
* 构造头部
*/
private Element createHeader(){
Element header = request.addElement("xs:element");
header.addAttribute("name", "Header");
return header;
}
/**
* 构造Body
*/
private Element createBody(){
Element body = request.addElement("xs:element");
body.addAttribute("name", "Body");
return body;
}
/**
* 构造Body
*/
private Element createRequest(){
Element request = root.addElement("xs:element");
request.addAttribute("name", "Request");
//
Element complexType=request.addElement("xs:complexType");
Element sqeuence=complexType.addElement("xs:sequence");
//
header=sqeuence.addElement("xs:element");
header.addAttribute("name", "Header");
//
body=sqeuence.addElement("xs:element");
body.addAttribute("name", "Body");
return request;
}
/**
* 新增复合元素
* @param typeName 元素名
* @param elementNodes 元素节点名
* @return
*/
public void addComplexType(String typeName,List elementNodes){
Element complexType=root.addElement("xs:complexType");
complexType.addAttribute("name", typeName);
Element sqeuence=complexType.addElement("xs:sequence");
//添加元素
for (int i = 0; i < elementNodes.size(); i++) {
Element ele=sqeuence.addElement("xs:element");
ele.addAttribute("name", elementNodes.get(i).getName());
ele.addAttribute("type", elementNodes.get(i).getType());
}
}
//=======================================getter setter
/**
* 获取头部
* @param complexTypeName 复合类型名称
* @return
*/
private Element initHeader(String complexTypeName) {
header.addAttribute("type", complexTypeName);
List list=root.selectNodes("./xs:complexType[@name='"+complexTypeName+"']");
if(list.size()==0){
throw new RuntimeException("不存在复合类型\""+complexTypeName+"\"请选择正确的复合类型名.");
}
return header;
}
/**
* 获得Body元素
* @param complexTypeName 复合类型名称
* @return
*/
private Element initBody(String complexTypeName) {
body.addAttribute("type", complexTypeName);
List list=root.selectNodes("./xs:complexType[@name='"+complexTypeName+"']");
if(list.size()==0){
throw new RuntimeException("不存在复合类型\""+complexTypeName+"\"请选择正确的复合类型名.");
}
return body;
}
/**
* 初始化Header与Body
* @param headerComplexTypeName 头部复合名称
* @param bodyComplexTypeName body复合名称
*/
public void initHeaderBody(String headerComplexTypeName,String bodyComplexTypeName){
initHeader(headerComplexTypeName);
initBody(bodyComplexTypeName);
}
/**
* 改变元素复合类型名称
* @param xPathQuery
* @param complexTypeName
*/
public void changeElementTypeName(String xPathQuery,String complexTypeName){
List list=root.selectNodes(xPathQuery);
if(list.size()==0){
throw new RuntimeException("没有搜索到"+xPathQuery+"路径下的任何元素.");
}
if(list.size()>1){
throw new RuntimeException("在此路径下"+xPathQuery+"搜索到多个元素.");
}
Element element=(Element) list.get(0);
element.addAttribute("type", complexTypeName);
}
/***
* 写出
*/
public void writeToDisk(){
try {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter(
new File("test.xsd")),format);
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
调用代码示例:
XSDGenerate generate=new XSDGenerate();
generate.addComplexType("User", Arrays.asList(new ElementNode("id"),new ElementNode("name"),new ElementNode("role","Role")));
generate.addComplexType("Role", Arrays.asList(new ElementNode("id"),new ElementNode("roleName")));
generate.addComplexType("Organization", Arrays.asList(new ElementNode("id"),new ElementNode("orgName")));
generate.initHeaderBody("User", "Organization");
// generate.changeElementTypeName("./xs:complexType[@name='User']/xs:sequence/xs:element[@name='role']", "Role");
generate.writeToDisk();
生成结果: