First of all, load file whenever you need and unload it when you're done with it. Do not load the file onResourceStart and unload it onResourceStop, you may have problems when server crash.
Second of all, paste long codes on http://pastebin.com
You should debug the code by outputing messages (outputChatBox or outputDebugString (with debugscript window enabled)). As you can see the errors say "bad argument", that means the argument passed to the function does not match the argument it expects. Some functions returns elements and false (if it fails), so check if the returned value is not false, especially when working with XML.