0
我試圖從MySQL數據庫中檢索對象並將其存儲在XML文件中。下面的代碼是不是以我需要的格式編寫XML數據。如何在從MySQL數據庫中提取數據時在XML文件中追加多個子根節點
private static Document buildCustomerXML(ResultSet EmployeeRS) throws Exception
{
Document xmlDoc = new DocumentImpl();
/* Creating the root element */
Element rootElement = xmlDoc.createElement("Schedule");
xmlDoc.appendChild(rootElement);
while(EmployeeRS.next())
\t {
\t Element employee1 = xmlDoc.createElement("Employees");
\t \t Element employee = xmlDoc.createElement("Employee");
\t \t /* Build the CustomerId as a Attribute*/
\t \t employee.setAttribute("id", EmployeeRS.getString("id"));
\t \t /* Creating elements within customer DOM*/
\t \t Element contractid = xmlDoc.createElement("ContractID");
\t \t Element lastName = xmlDoc.createElement("Name");
\t \t Element skills = xmlDoc.createElement("Skills");
\t \t Element skill = xmlDoc.createElement("Skill");
\t \t /* Populating Customer DOM with Data*/
\t \t contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid")));
\t \t lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last")));
\t \t
\t \t skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill")));
\t \t
\t \t /* Adding the firstname and lastname elements to the Customer Element*/
\t \t employee.appendChild(contractid);
\t \t employee.appendChild(lastName);
\t \t employee.appendChild(skills);
\t \t skills.appendChild(skill);
\t \t employee1.appendChild(employee);
\t \t rootElement.appendChild(employee1);
\t \t
\t \t
\t \t
\t \t /* Appending Customer to the Root Class*/
\t \t
\t \t
\t \t /* Build the CustomerId as a Attribute*/
\t \t Element constraints1 = xmlDoc.createElement("Constraints");
\t \t Element constraints = xmlDoc.createElement("Constraint");
\t \t constraints.setAttribute("id", EmployeeRS.getString("id"));
\t \t Element startdat = xmlDoc.createElement("startdate");
\t \t startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate")));
\t \t constraints.appendChild(startdat);
\t \t constraints1.appendChild(constraints);
\t \t
\t \t rootElement.appendChild(constraints1);
\t \t
\t }
\t return xmlDoc;
\t }
下面是輸出即時得到用於上述代碼
<?xml version="1.0" encoding="UTF-8"?>
<Schedule>
<Employees>
<Employee id="11">
<ContractID>1</ContractID>
<Name>kumbhar</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="11">
<startdate>11/08/2014</startdate>
</Constraint>
</Constraints>
<Employees>
<Employee id="14">
<ContractID>1</ContractID>
<Name>Raje</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="14">
<startdate>2014-11-12</startdate>
</Constraint>
</Constraints>
</Schedule>
但是我需要輸出是在下面的表格
<?xml version="1.0" encoding="UTF-8"?>
<Schedule>
<Employees>
<Employee id="11">
<ContractID>1</ContractID>
<Name>kumbhar</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
<Employee id="14">
<ContractID>1</ContractID>
<Name>Raje</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="14">
<startdate>2014-11-12</startdate>
</Constraint>
<Constraint id="11">
<startdate>11/08/2014</startdate>
</Constraint>
</Constraints>
</Schedule>
任何一個可以給我提供關於如何實現輸出按上述格式