Jump to content

[HELP] Problem with xmlLoadFile.


Recommended Posts

Hi,

I'm trying to load an XML file. I did everything right, but it isn't working.

Code:

local cFile = xmlLoadFile("connect.xml")
if cFile == false or cFile == nil then
outputChatBox("Unable to open connect.xml.", source)
triggerClientEvent(source, "showRegisterWindow", source)
return
end

I use xmlGetNodeAttribute on the cFile to retrieve the attributes from it.

This is the code where it loads the file. In my resource folder is the file connect.xml:

<connect hostname="****" username="*****" password="*****" database="*******" />

I hope this is enough information to solve my problem.

Link to comment

Yes that what it does. And if I delete that lines where it checks if it's nill or false, it gives a Bad Argument error at xmlGetNodeAttribute, some lines underneath it. So the problem is really the xmlLoadFile....

I really don't get it. It always worked normally, but with the new 1.0.4 it doesn't work..

Link to comment

Please can someone help me? I really want to use 1.0.4, since it seems that the new version causes the problem. I tried upgrading, debugging, but it's no use... :(

EDIT: It seems that the new version isn't the problem. I just don't get it, first it worked, now it isn't working anymore :o.

Link to comment

Try this.

Before doing the xmlLoadFile, output to chat fileExists("connect.xml") just to make sure MTA recognises the file.

If it returns true, try having the xml file as

<connect></connect>

instead of just

<connect/>

Maybe it wants the root node to be like that. Also maybe it doesn't like attributes for the root node. Just see if it loads the file. These are just "maybes" which you should try. I'm not really sure what could be the problem there.

EDIT: And just a tip, instead of checking, if the file is nil and if the file is false, use just

if not File then end

, because not nil and not false both return true

Link to comment

Many thanks for your reply, I'll try your suggestions now :).

FileExists returns True, changed to , changed everything right, now looks like this:

<connect>
<hostname text="">
<username text="">
<password text="">
<database text=""> 
</connect>

Code to retrieve data:

local cFile = xmlLoadFile("connect.xml")
if not cFile then
outputChatBox("Unable to open connect.xml.", source)
triggerClientEvent(source, "showRegisterWindow", source)
return
end
hostChild = xmlFindChild(cFile, "hostname", 0)
sqluserChild = xmlFindChild(cFile, "username", 0)
sqlpassChild = xmlFindChild(cFile, "password", 0)
databaseChild = xmlFindChild(cFile, "database", 0)
hostname = xmlNodeGetAttribute(hostChild, "text")
sqlusername = xmlNodeGetAttribute(sqluserChild, "text")
sqlpassword = xmlNodeGetAttribute(sqlpassChild, "text")
database = xmlNodeGetAttribute(databaseChild, "text")

And underneath I check if it's there. Well you may guess, it returns: Unable to open connect.xml. Now I'm wondering, maybe I miss something in my windows? Like a .dll or something? I don't know if it uses dll's to open xml files, but maybe that's the problem.

Link to comment

I checked the server directory and there is a file called xml.dll. Maybe that's just the problem, that I got a damaged one. I'm now going to try to load another file, maybe that works.

I tried to load another file,

<test>
<noob text="noob" />
</test>

with:

function loadXML(player, command)
outputChatBox(tostring(fileExists("test.xml")))
myFile = xmlLoadFile("test.xml")
if myFile then
	nChild = xmlFindChild(myFile, "noob", 0)
if nChild then
		theNoob = xmlNodeGetAttribute(nChild, "text")
if theNoob then
outputChatBox(tostring(theNoob))
xmlUnloadFile(myFile)
else
outputChatBox("Unable to get attribute")
xmlUnloadFile(myFile)
end
else
outputChatBox("Unable to find child")
xmlUnloadFile(myFile)
end
else
outputChatBox("unable to open test.xml")
end
end
addCommandHandler("xmltest", loadXML)

And in the chatbox I get: true and noob.... So I really wonder what the problem is in my other script... :o

EDIT: I re-checked my other posts and I saw that I had in my connect.xml the lines hostname="" ending with >, not with />. So now I'm going to try if that solves the problem.

EDIT2: Didn't solve it.

EDIT3: I load the function from Client side, with triggerServerEvent, can that cause the problem?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...