How to solve XML reffers to not existing parent error of dhtmlxTree
When I use dhtmlxTree dynamical loading with jsp, I always get the following exception:
Error type: DataStructure
Description: XML reffers to not existing parent
The code of the jsp page is as following:
---------------------------------------------------------------------------------------------------------------
<html>
<head>
</head>
<body>
<link rel="STYLESHEET" type="text/css" href="css/dhtmlxtree.css">
<script src="js/dhtmlxcommon.js"></script>
<script src="js/dhtmlxtree.js"></script>
<div id="treeboxbox_tree" style="width:100%; height:218;background-color:#f5f5f5;border :1px solid Silver;; overflow:auto;"></div>
<script type="text/javascript">
tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
tree.attachEvent("onClick",onNodeSelect);
tree.setImagePath("images/tree/");
tree.setXMLAutoLoading("AdminFolderServlet?action=folder_treeview");
tree.loadXML("AdminFolderServlet?action=folder_treeview&id=1");
function onNodeSelect(nodeId){
document.getElementById("parent_folder_id").value = nodeId;
}
</script>
</body>
</html>
---------------------------------------------------------------------------------------------------------------
And the following is the code snippet used to generate XML file of the tree:
---------------------------------------------------------------------------------------------------------------
String id = request.getParameter("id");
if (id == null) id = "1";
Vector folders = Folder.getFolders(Integer.parseInt(id));
StringBuffer buff = new StringBuffer("< ?xml version=\"1.0\"?>“);
buff.append(”
“);
for (int i = 0; i < folders.size(); i++) {
Folder f = (Folder)folders.get(i);
buff.append("“);
}
buff.append(”“);
response.setContentType(”text/xml”);
response.setHeader(”Cache-Control”,”no-cache”); //HTTP 1.1
response.setHeader(”Pragma”,”no-cache”); //HTTP 1.0
response.setDateHeader(”Expires”, 0); //prevents caching at the proxy server
response.getWriter().write(buff.toString());
---------------------------------------------------------------------------------------------------------------
The generated XML is well formed.
---------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<tree id='1'>
<item child='1' id='2' text='Games' im0='folderClosed.gif' im1='folderOpen.gif' im2='folderClosed.gif'></item>
<item child='1' id='3' text='Music' im0='folderClosed.gif' im1='folderOpen.gif' im2='folderClosed.gif'></item>
<item child='0' id='4' text='Books' im0='folderClosed.gif' im1='folderOpen.gif' im2='folderClosed.gif'></item>
<item child='1' id='5' text='Movies' im0='folderClosed.gif' im1='folderOpen.gif' im2='folderClosed.gif'></item>
<item child='1' id='6' text='Photos' im0='folderClosed.gif' im1='folderOpen.gif' im2='folderClosed.gif'></item>
<item child='0' id='14' text='Articles' im0='folderClosed.gif' im1='folderOpen.gif' im2='folderClosed.gif'></item>
</tree>
---------------------------------------------------------------------------------------------------------------
I finally found the solution. The id attribute of the tree element must point to the parent id, which data will be linked, by default super-root ID = 0, but it can be changed while tree initialization ( 4th parameter of constructor ), In case of dynamic loading the tree id must be equal to the parent ID for which data requested.
In my database, the root id of the top level folder is 1, however, I use 0 in the tree initialization method.
---------------------------------------------------------------------------------------------------------------
tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0);
---------------------------------------------------------------------------------------------------------------
So, I got the exception. After changing the fourth parameter to 1, it works well.
tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",1);
---------------------------------------------------------------------------------------------------------------