Error while creating XML file in JAVA



Am reading data from MySQL database and writing it to XML file. AM getting following error "HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. " Any Ideas how to solve this





private static Document buildCustomerXML(ResultSet _customerRS, ResultSet contractRS) throws Exception
{

Document xmlDoc = new DocumentImpl();

/* Creating the root element */

Element rootElement1 = xmlDoc.createElement("Contracts");
xmlDoc.appendChild(rootElement1);
while(contractRS.next()){
Element contract = xmlDoc.createElement("Contract");

/* Build the CustomerId as a Attribute*/
contract.setAttribute("ID", _customerRS.getString("ID"));
rootElement1.appendChild(contract);
}
Element rootElement = xmlDoc.createElement("Employees");
xmlDoc.appendChild(rootElement);
while(_customerRS.next())
{

Element employee = xmlDoc.createElement("Employee");

/* Build the CustomerId as a Attribute*/
employee.setAttribute("id", _customerRS.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(_customerRS.getString("Contractid")));
lastName.appendChild(xmlDoc.createTextNode(_customerRS.getString("last")));

skill.appendChild(xmlDoc.createTextNode(_customerRS.getString("Skill")));

/* Adding the firstname and lastname elements to the Customer Element*/
employee.appendChild(contractid);
employee.appendChild(lastName);

skills.appendChild(skill);
employee.appendChild(skills);



/* Appending Customer to the Root Class*/
rootElement.appendChild(employee);
}
return xmlDoc;
}



No comments:

Post a Comment