Encoding IDs in dhtmlXTree

Hi,



I am using dhtmlXtree to represent a file system tree, and need to be able to use the actual file system path of a node as it’s ID (rather than an integer). This is so that when the “open” event calls the load function (for dynamically loading from XML), my server code knows the file system path to load.



The problem I am having is with invalid XML characters (eg ampersand) in folder names on the file system causing an error in the tree. I have tried encoding relevant characters in the XML response (for the ID attribute) sent by my server code, but somehow dhtmlXTree is stll barfing when it encounters an ampersand in an ID.



Is there any was we can engineer dhtmlXTree to (optionally) xml-encode node IDs? I know from previous experience that Javascript will often auto-decode encoded entities which I suspect is the cause of the problem.



Any thoughts/suggestions?



Thanks,



Jason.



P.S.



As soon as I can get everything working I am planning to buy the Pro version :slight_smile:

If you are using & in XML it will be automatically decoded by XML parser back to the &, so it may be the reason of problem.
Technically you can try to use

  tree.setEscapingMode(“utf8”);

It will enable different data escaping logic, which must process all chars correctly ( including problematic & )


Thanks for this, it has indeed solved the encoding problem, but now I have a different error.



I am using dynamic loading of child nodes using xml, and when my server code is called the node ID which it is provided (which is my file system path) is incomplete.



That is, if my file system path is something like “C:\Test\Test & Test”, my server code receives "C:\Test\Test " as the path.



I am encoding the ID in the XML I return to produce the list of children (change & to &), and if I don’t do this I get an error in the tree (actually, Firefox reports a not well-formed error, presumably trying to parse the XML), and if I DO encode the XML coming back, I get this weird path truncation at the point of the ampersand.



Any ideas?



Thanks



 


Addendum,



Using Tamper Data in Firefox I see that the HTTP GET request sent is:



list.action?uid=1231390609688&id=C:%5Ctest%5Ctest%20&%20test&a_dhx_rSeed=1231390609688



You can see that the “&” in the middle of the ID is not URL encoded, and hence is treated as a parameter in the request, which explains why I am getting a truncated ID value on the server side.



 


Final Addenum:  I’m an idiot.



My escaping of XML at the server side was working correctly.  I had incorrectly missed the encoding of the root tree id.  I was encoding item IDs, but not the tree ID.



Of course when dhtmlXTree is loading children, it’s loading a new “tree” and hence the root ID of that tree needs to also be encoded.



Fixing this, and removing the previous setEscapingMode call makes everything work fine.



Sorry for the inconvenience.



However, I did notice that the _escape method in dhtmlxtree.js uses encodeURI() when utf8 escaping is set, but this is only called when escaping the ID component.  This call will encode everything EXCEPT ampersand.  There is an argument to say that this should be encodeURIComponent() which will encode ampersands aswell.



I don’t need it because I am encoding on the server side.  It’s weird though, because it actually shouldn’t work due to the “auto-decoding” done by the XML parser in the browser as previously discussed.  I will check browsers other than firefox, however it seems encoding on the server will work without setEscapingMode(“utf8”), but encoding on the client (ie in the tree) will only work if the _escape method is changed to use encodeURIComponent() (this is what I found anyway).



Thanks… and your components are awesome BTW.

if the _escape method is changed to use encodeURIComponent() (this is what I found anyway).
Problem confirmed , we will update escape logic to encodeURIComponent in next build
Thanks for your remark.