0
正在從MySQL數據庫讀取數據並將其寫入XML文件。 我得到以下錯誤「HIERARCHY_REQUEST_ERR:試圖插入其中,這是不允許的節點。」 任何想法如何解決這個在JAVA中創建XML文件時出錯
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()){
\t Element contract = xmlDoc.createElement("Contract");
\t /* Build the CustomerId as a Attribute*/
\t contract.setAttribute("ID", _customerRS.getString("ID"));
\t rootElement1.appendChild(contract);
}
Element rootElement = xmlDoc.createElement("Employees");
xmlDoc.appendChild(rootElement);
while(_customerRS.next())
{
\t
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;
}
可以請你給我一個例子,用多個結果集檢索不同表中的數據並寫入一個XML文件。 – 2014-11-10 07:41:14
你快到了。只需創建一個根元素,並將「合同」和「員工」添加爲兒童。 – Khalid 2014-11-10 10:28:54