fairyoggy Posted August 1, 2019 Share Posted August 1, 2019 Can you show me the working code with which I can edit an XML file and save it? I tried to use the code on the wiki, but something does not work There is a file: How do I use code: 1) Delete Attribute (Admin, Otvet) and save xml file 2) Add a new line with the same attributes and save xml file Link to comment
HassoN Posted August 1, 2019 Share Posted August 1, 2019 (edited) 1) local xml = xmlLoadFile("file.xml") -- your file name local children = xmlNodeGetChildren(xml) if children then for i, v in ipairs(children) do local name = xmlNodeGetName(v) if (name == "Admin" or name == "Otvet") then xmlDestroyNode(v) xmlSaveFile(xml) xmlUnloadFile(xml) return true end end end 2) local xml = xmlLoadFile("file.xml") local child = xmlCreateChild(xml, "report") xmlNodeSetAttribute(child, "vreamya", "1") xmlNodeSetAttribute(child, "Name", "2") xmlNodeSetAttribute(child, "Jaloba", "3") xmlSaveFile(xml) xmlUnloadFile(xml) Edited August 1, 2019 by HassoN 1 Link to comment
fairyoggy Posted August 1, 2019 Author Share Posted August 1, 2019 23 minutes ago, HassoN said: 1) local xml = xmlLoadFile("file.xml") -- your file name local children = xmlNodeGetChildren(xml) if children then for i, v in ipairs(children) do local name = xmlNodeGetName(v) if (name == "Admin" or name == "Otvet") then xmlDestroyNode(v) xmlSaveFile(xml) xmlUnloadFile(xml) return true end end end 2) local xml = xmlLoadFile("file.xml") local child = xmlCreateChild(xml, "report") xmlNodeSetAttribute(child, "vreamya", "1") xmlNodeSetAttribute(child, "Name", "2") xmlNodeSetAttribute(child, "Jaloba", "3") xmlSaveFile(xml) xmlUnloadFile(xml) The file does not change in any way. Link to comment
fairyoggy Posted August 1, 2019 Author Share Posted August 1, 2019 How to delete a specific line? <reports> <report vreamya="1" Name="1" Jaloba="1" Admin="1" Otvet="1"/> <report vreamya="2" Name="2" Jaloba="3" Admin="4" Otvet="5"/> </reports> Example line - 2 <report vreamya="2" ... Link to comment
HassoN Posted August 1, 2019 Share Posted August 1, 2019 37 minutes ago, slapz0r said: The file does not change in any way. Which code are you talking about? Try to be more precise in your issue. 12 minutes ago, slapz0r said: How to delete a specific line? <reports> <report vreamya="1" Name="1" Jaloba="1" Admin="1" Otvet="1"/> <report vreamya="2" Name="2" Jaloba="3" Admin="4" Otvet="5"/> </reports> Example line - 2 <report vreamya="2" ... xmlFindChild -- line 2 xmlDestroyNode -- destroy it. Link to comment
fairyoggy Posted August 2, 2019 Author Share Posted August 2, 2019 1 hour ago, HassoN said: Which code are you talking about? Try to be more precise in your issue. xmlFindChild -- line 2 xmlDestroyNode -- destroy it. how to read the updated file? I mean 1) Added a new line to the file 2) Clear GridList 3) LoadFile But when I upload the file to the grid, there are no new lines, but they were saved to the file --client addEventHandler("onClientGUIClick", root, function () if ( source == buy_report ) then refreshrr() end end end) function refreshrr () guiGridListClear(gridSms) local node = xmlLoadFile ( "reports.xml" ) if ( node ) then local reports = 0 while ( xmlFindChild ( node, "report", reports ) ~= false ) do local report = xmlFindChild ( node, "report", reports ) local row = guiGridListAddRow(gridSms) guiGridListSetItemText(gridSms, row, columnInt1, xmlNodeGetAttribute(report, "vremya" ), false, true ) guiGridListSetItemText(gridSms, row, columnInt2, xmlNodeGetAttribute(report, "Name" ), false, false ) reports = reports + 1 end xmlUnloadFile(node) end end --server function repotvet (hours, minutes, second, name) local xml = xmlLoadFile("reports.xml") local child = xmlCreateChild(xml, "report") xmlNodeSetAttribute(child, "vremya", ""..hours..":"..minutes..":"..second.."") xmlNodeSetAttribute(child, "Name", name) xmlSaveFile(xml) xmlUnloadFile(xml) end addEvent("repotvet", true) addEventHandler("repotvet", resourceRoot, repotvet) Link to comment
HassoN Posted August 2, 2019 Share Posted August 2, 2019 As I can see it is a report system, and you are trying to load the file from client side. That's totally unsafe, people can modify the content of that file. Use only server side and you may stay update your gridlist by using a trigger. Link to comment
fairyoggy Posted August 4, 2019 Author Share Posted August 4, 2019 On 02/08/2019 at 13:11, HassoN said: As I can see it is a report system, and you are trying to load the file from client side. That's totally unsafe, people can modify the content of that file. Use only server side and you may stay update your gridlist by using a trigger. How to do it? How to open a saved option? Link to comment
HassoN Posted August 5, 2019 Share Posted August 5, 2019 2 hours ago, slapz0r said: How to do it? How to open a saved option? Use: local xml = xmlLoadFile("file.xml") -- loads the file local child = xmlFindChild(xml, "report", 1) -- 1 here represents the index local myData = xmlNodeGetAttribute(child, "vreamya") -- gets the value of "vreamya" xmlUnloadFile(xml) -- unloads the file Link to comment
fairyoggy Posted August 5, 2019 Author Share Posted August 5, 2019 44 minutes ago, HassoN said: Use: local xml = xmlLoadFile("file.xml") -- loads the file local child = xmlFindChild(xml, "report", 1) -- 1 here represents the index local myData = xmlNodeGetAttribute(child, "vreamya") -- gets the value of "vreamya" xmlUnloadFile(xml) -- unloads the file i use it in "refreshrr" function but didn'e work Link to comment
fairyoggy Posted August 5, 2019 Author Share Posted August 5, 2019 (edited) I mean, the original file loads normally, but when you add a new record, it is saved to the file, but the updated file does not output to the gridlist. If you restart the script, a new record will appear, but if you add another new one then it will not be displayed in the gridlist(but in file saved successfully) See my code above. How to fix it? Edited August 5, 2019 by slapz0r Link to comment
HassoN Posted August 5, 2019 Share Posted August 5, 2019 Because you update your XML on server side, then you load it from client, therefore it is not updated. You should load it from server, check this out: -- client function refreshrr() triggerServerEvent("refreshList", resourceRoot) end function sendInfo(myTable) guiGridListClear(gridSms) for i, v in ipairs(myTable) do local row = guiGridListAddRow(gridSms) guiGridListSetItemText(gridSms, row, columnInt1, v[1], false, true ) guiGridListSetItemText(gridSms, row, columnInt2, v[2], false, false ) end end addEvent("sendInfo", true) addEventHandler("sendInfo", root, sendInfo) -- server function refreshList() local node = xmlLoadFile("reports.xml") if (not node) then return false end myTable = {} local reports = 0 while ( xmlFindChild(node, "report", reports) ~= false ) do local report = xmlFindChild ( node, "report", reports ) table.insert(myTable, {xmlNodeGetAttribute(report, "vremya"), xmlNodeGetAttribute(report, "Name")}) reports = reports + 1 end xmlUnloadFile(node) if (#myTable > 0) then triggerClientEvent(client, "sendInfo", client, myTable) end end addEvent("refreshList", true) addEventHandler("refreshList", root, refreshList) 1 Link to comment
fairyoggy Posted August 6, 2019 Author Share Posted August 6, 2019 On 02/08/2019 at 02:07, HassoN said: Which code are you talking about? Try to be more precise in your issue. xmlFindChild -- line 2 xmlDestroyNode -- destroy it. How to use it correctly? Example: There are 2 lines <report vreamya="1" Name="1" Jaloba="1" Admin="1" Otvet="1"/> <report vreamya="1" Name="2" Jaloba="1" Admin="1" Otvet="1"/> How to find exactly the 2nd line and make changes in it? I mean, how to find the 2nd line and change it, for example, Jaloba = "2" And the second line will be <report vreamya="1" Name="2" Jaloba="2" Admin="1" Otvet="1"/> Link to comment
HassoN Posted August 6, 2019 Share Posted August 6, 2019 12 hours ago, slapz0r said: How to use it correctly? Example: There are 2 lines <report vreamya="1" Name="1" Jaloba="1" Admin="1" Otvet="1"/> <report vreamya="1" Name="2" Jaloba="1" Admin="1" Otvet="1"/> How to find exactly the 2nd line and make changes in it? I mean, how to find the 2nd line and change it, for example, Jaloba = "2" And the second line will be <report vreamya="1" Name="2" Jaloba="2" Admin="1" Otvet="1"/> Same as the example above xmlFindChild -- use index 1 this time as you want to modify your 2nd line. xmlNodeSetAttribute -- modify the text you want Link to comment
fairyoggy Posted August 7, 2019 Author Share Posted August 7, 2019 5 hours ago, HassoN said: Same as the example above xmlFindChild -- use index 1 this time as you want to modify your 2nd line. xmlNodeSetAttribute -- modify the text you want I understood this, but I need an example of how to use this in the code to change the selected line. I wouldn’t ask it a second time, if that was enough Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now