【发布时间】:2014-11-14 19:14:39
【问题描述】:
我正在尝试从 MySQL 数据库中检索对象并将其存储在 XML 文件中。下面的代码如何没有以我需要的格式写入 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())
{
Element employee1 = xmlDoc.createElement("Employees");
Element employee = xmlDoc.createElement("Employee");
/* Build the CustomerId as a Attribute*/
employee.setAttribute("id", EmployeeRS.getString("id"));
/* Creating elements within customer DOM*/
Element contractid = xmlDoc.createElement("ContractID");
Element lastName = xmlDoc.createElement("Name");
Element skills = xmlDoc.createElement("Skills");
Element skill = xmlDoc.createElement("Skill");
/* Populating Customer DOM with Data*/
contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid")));
lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last")));
skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill")));
/* Adding the firstname and lastname elements to the Customer Element*/
employee.appendChild(contractid);
employee.appendChild(lastName);
employee.appendChild(skills);
skills.appendChild(skill);
employee1.appendChild(employee);
rootElement.appendChild(employee1);
/* Appending Customer to the Root Class*/
/* Build the CustomerId as a Attribute*/
Element constraints1 = xmlDoc.createElement("Constraints");
Element constraints = xmlDoc.createElement("Constraint");
constraints.setAttribute("id", EmployeeRS.getString("id"));
Element startdat = xmlDoc.createElement("startdate");
startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate")));
constraints.appendChild(startdat);
constraints1.appendChild(constraints);
rootElement.appendChild(constraints1);
}
return xmlDoc;
}
下面是上面代码的输出
<?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>
谁能给我一些关于如何实现上述格式输出的建议
【问题讨论】:
标签: mysql xml dom xml-serialization xstream