How to append multiple child root nodes in an XML file while fetching data from MySQL Database



Am trying to retrieve objects from MySQL database and store it in an XML file. How ever the below code is not writing the XML data in the format i needed.





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



Below is the output im getting for the above code





<?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>



However i need output to be in the below form





<?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>



Can any one provide me suggestions on how to achieve output in the above format


No comments:

Post a Comment