2016-06-01 8 views
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> 

答えて

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()); 
} 

はとにかくありがとうございます!

関連する問題