Jump to content

xmlLoadFile can't find file


dugasz1

Recommended Posts

Hello guys!

I have a weir issu (or just i don't see what is the problem)
So i wanted to write a script which saves a table. But if i call my saveTable method it doesn't load the xml file just create one then overwrite it. And i don't have a clue why...
 

function open( path, version )
    --outputDebugString("XML: open called", 1)
    outputDebugString(rootNode, 1)
    rootNode = xmlLoadFile(path)
    outputDebugString(rootNode, 1)
    if (not rootNode) then
    outputDebugString(path)
        rootNode = xmlCreateFile(path, "root")
        xmlNodeSetAttribute(rootNode, "version", version)
        xmlCreateChild(rootNode, "tables")
        outputDebugString("XML: rootNode created", 2)
    else
        outputDebugString("XML: rootNode exist",2)
        local _version = xmlNodeGetAttribute (rootNode, "version")
        if(tostring(version) ~= _version) then
            close()
            outputDebugString("XML: Deleted",2)
            fileDelete(path)
            open(path, version)
        end
    end
end

function close( )
    xmlSaveFile (rootNode)
    xmlUnloadFile (rootNode)
    rootNode = nil
    outputDebugString("XML: Save and Unload")
end

function saveTable (path, table, name, version)
    open(path, version)
    outputDebugString(rootNode)
    local tableNode = xmlCreateChildIfNotExist(xmlFindChild(rootNode, "tables", 0), name)
    outputDebugString(rootNode)
    saveTableRecursive(path, table, name, tableNode)
    outputDebugString(rootNode)
    close()
    outputDebugString("XML: Table lementve")
end

function saveTableRecursive (path, table, name, node)
    for k,v in pairs(table) do
        local vType = type(v)
        if (vType == "table") then
            local newNode = xmlCreateChild(node, k)
             outputDebugString("XML: Table recurssive")
             saveTableRecursive (path, v, name, newNode)
        elseif( (vType == "string") or (vType == "number") or (vType == "boolean") ) then
            local insertValue = xmlCreateChild(node, k)
            xmlNodeSetAttribute(insertValue, "type", vType)
            xmlNodeSetValue(insertValue, tostring(v))
        else
            outputDebugString("XML: Not supported type: "..vType, 1)
        end
    end
end


function xmlCreateChildIfNotExist(parentNode, tagName)
    local child = xmlFindChild(parentNode, tagName, 0)

    if not child then
        child = xmlCreateChild(parentNode, tagName)
    end

    return child
end




local asd = {
    {x=2, y=5, width=100, height = 200},
    {x=44, y=50, width=100, height = 30},
    ["test"] = {x=2, y = 5, width = 100, height = false},
}

saveTable(":xml/test1.xml", asd, "tesztTable", "1.0")
saveTable(":xml/test1.xml", asd, "tesztTable2", "1.0")

It fails on line 4. rootNode will be false. What do i do wrong or it is a known issue?

PS.:
If a got to the file open it and delete a few rows it founds it somewhy.

Link to comment
  • Administrators

Where is the test1.xml file located? Is it in the same resource, inside the xml folder?

26 minutes ago, dugasz1 said:
  • saveTable(":xml/test1.xml", asd, "tesztTable", "1.0")
  • saveTable(":xml/test1.xml", asd, "tesztTable2", "1.0")

If so, remove the colons before the filepath

Edited by LopSided_
Link to comment
  • Administrators
9 minutes ago, dugasz1 said:

:xml stands for the path of the xml resource. So the file is in the xml resource's root directory.

Ah good. Just checking it wasn't that :P

So let me just understand things properly... the rootNode is returning false?

 

 

Link to comment
  • Administrators
15 minutes ago, dugasz1 said:

At line 4 xmlLoadFile return with false. Like it's not found the file but it's there. So the rootNode variable become false.

Ah okay, I get you now. I was slightly confused on what the actual problem was.

Have you tried including test1.xml in your meta.xml?


<config src="xml/test1.xml" type="client" />

 

Edited by LopSided_
Link to comment
  • Administrators
35 minutes ago, dugasz1 said:

There are no errors. As i mentioned it can't load it but if i edit the content of the test1.xml it load it once, after again can't load it.

Don't know if you've already crossed this bridge, but have you given ACL rights for "ModifyOtherObjects" on this resource?

Also check line 3, 4, 5 in your original reply (shown below)

outputDebugString(rootNode, 1) -- rootNode doesn't exist yet
rootNode = xmlLoadFile(path)
outputDebugString(rootNode, 1)

 

Edited by LopSided_
Link to comment
  • Administrators

Check my updated reply also.

This resource should require general.ModifyOtherObjectsbecause it is editing files outside of its own, in another resource.

I'm not sure why you aren't receiving errors about it, I loaded this up and had lots of errors, especially about the ACL

Edited by LopSided_
Link to comment

When i call first the function i know that the rootNode is nill. 
But after i call second time the function it is a userdata (I don't know why because i unload it )
It was just for debuging 

Edit:

9 minutes ago, LopSided_ said:

Check my updated reply also.

This resource should require general.ModifyOtherObjectsbecause it is editing files outside of its own, in another resource.

I'm not sure why you aren't receiving errors about it, I loaded this up and had lots of errors, especially about the ACL

I give them admin rights but still nothing. 
(It edit it own file yet but i used this kind of path beacause it will be exported. So i guess this is why it don't show error)

Edited by dugasz1
Link to comment
  • Administrators

Just spent some time testing everything, got it to work but as you said, "tesztTable2" is overwriting "tesztTable", not appendind to it. I'd just suggest using a library like http://viremo.eludi.net/LuaXML/ - it comes with append functions, so you can just read in data, add to it, and save it again, much easier than what you're using at the moment.

Edited by LopSided_
Link to comment

You're defining the rootNode globally in open() then you'r nilling it in close(), so it will recreate the file everytime.

Just change it to local and remove rootNote = nil in close().

 

Edit: Or just leave it global and remove the line in close(), OR better, return the rootNode in open() and use it to close it: close(rootNode)

Edited by Tails
  • Like 1
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...