1
次のコードは、XSDファイル(SQL.xsd)に基づいてJavaファイル(QUERY.java)を作成します。JavaおよびXML(アンマーシャリング)を使用してプロパティを持つオブジェクトを作成する
String directory = "D:/PROJEKTE/2016/";
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("de.example.jaxb");
File myXsd = new File("SQL.xsd");
InputSource source = new InputSource(myXsd.toURI().toString());
sc.parseSchema(source);
S2JJAXBModel model = sc.bind();
JCodeModel jCodeModel = model.generateCode(null, null);
jCodeModel.build(new File(directory));
QUERY.java:
public class QUERY {
@XmlElement(name = "SELECT", required = true)
protected QUERY.SELECT select;
@XmlElement(name = "TABLE")
protected List<QUERY.TABLE> table;
@XmlElement(name = "JOIN")
protected List<QUERY.JOIN> join;
@XmlElement(name = "WHERE", required = true)
protected String where;
@XmlElement(name = "GROUP", required = true)
protected QUERY.GROUP group;
@XmlElement(name = "ORDER", required = true)
protected QUERY.ORDER order;
@XmlAttribute(name = "author")
protected String author;
public QUERY.SELECT getSELECT() {
return select;
}
public void setSELECT(QUERY.SELECT value) {
this.select = value;
}
public List<QUERY.TABLE> getTABLE() {
if (table == null) {
table = new ArrayList<QUERY.TABLE>();
}
return this.table;
}
public List<QUERY.JOIN> getJOIN() {
if (join == null) {
join = new ArrayList<QUERY.JOIN>();
}
return this.join;
}
public String getWHERE() {
return where;
}
public void setWHERE(String value) {
this.where = value;
}
public QUERY.GROUP getGROUP() {
return group;
}
public void setGROUP(QUERY.GROUP value) {
this.group = value;
}
public QUERY.ORDER getORDER() {
return order;
}
public void setORDER(QUERY.ORDER value) {
this.order = value;
}
public String getAuthor() {
return author;
}
public void setAuthor(String value) {
this.author = value;
}
...
は、残念ながら、私は(代わりに建物の)メモリでこれを保存する方法を、知りません。しかし、それぞれのXMLファイルを使ってオブジェクトにそのプロパティを与えることが不可欠です(Unmarshalling!)。
SQL.xml:
<?xml version="1.0" encoding="UTF-8"?>
<QUERY author='Test'>
<SELECT>
<COLUMN alias="id">
<ATTRIBUTE table="orders">OrderID</ATTRIBUTE>
</COLUMN>
<COLUMN>
<ATTRIBUTE table="customers">CustomerName</ATTRIBUTE>
</COLUMN>
<COLUMN>
<ATTRIBUTE table="orders">OrderDate</ATTRIBUTE>
</COLUMN>
<COLUMN alias="group">'Bestellungen'</COLUMN>
</SELECT>
<TABLE alias="orders">Orders</TABLE>
<TABLE alias="customers">Customers</TABLE>
<TABLE alias="product">Product</TABLE>
<JOIN type="INNER">
<LEFT table="orders">CustomerID</LEFT>
<RIGHT table="customers">CustomerID</RIGHT>
</JOIN>
<JOIN type="LEFT">
<LEFT table="orders">ProductID</LEFT>
<RIGHT table="product">ProductID</RIGHT>
</JOIN>
<WHERE>
CustomerName='Test'
</WHERE>
<GROUP>
<ATTRIBUTE>CustomerName</ATTRIBUTE>
<HAVING>SUM(Units)>30</HAVING>
</GROUP>
<ORDER>
<COLUMN>OrderID</COLUMN>
<SORT>DESC</SORT>
</ORDER>
</QUERY>