以下代码根据我的XSD文件(SQL.xsd)创建Java文件(QUERY.java)。
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>
答案 0 :(得分:0)
<强>解决方案:强>
File myXml = new File("SQL.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(myXml);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("QUERY");
for (int i = 0; i < nList.getLength(); i++)
{
Node nNode = nList.item(i);
Element eElement = (Element) nNode;
System.out.println("SELECT " + eElement.getElementsByTagName("SELECT").item(0).getTextContent());
System.out.println("FROM " + eElement.getElementsByTagName("TABLE").item(0).getTextContent());
System.out.println("JOIN " + eElement.getElementsByTagName("JOIN").item(0).getTextContent());
System.out.println("WHERE " + eElement.getElementsByTagName("WHERE").item(0).getTextContent());
System.out.println("GROUP " + eElement.getElementsByTagName("GROUP").item(0).getTextContent());
System.out.println("ORDER " + eElement.getElementsByTagName("ORDER").item(0).getTextContent());
}
无论如何,谢谢!