SpecT Posted April 7, 2015 Share Posted April 7, 2015 (edited) Hello again guys ... I hope that I won't get hated for that but ... yeah. So I'm kinda newbie with the xml scripting. I was looking at another scripts to try to get it but I just can't. I want to use a xml file to storage songs - song name, length and played times. The xml file (example) should looks like this: "Feint And Boyinaband - Time Bomb" length="3:32" played="2" /> Could you guys help me with it ? There will be needed 2 things: 1) Load the songs from the xml then send it to the client as a table 2) Edit the played value. I hope that I'm not too impudent and you guys are gonna help me... @If I'm not allowed to ask such things I will delete the topic! Edited April 9, 2015 by Guest Link to comment
xXMADEXx Posted April 7, 2015 Share Posted April 7, 2015 You can try to follow how I did it in my Nerd Gaming mp3 player script. https://github.com/braydondavis/Nerd-Ga ... client.lua Use xmlLoadFile to load the file, then xmlNodeGetChildren to get all the "song" children, then xmlNodeGetAttribute to get the name, length, and played. This is the function that I used in the NG mp3 player to load the xml radio stations: function getRadioStations ( ) local data = { } local file = xmlLoadFile ( "@xml/usermusic.xml" ) or createUserMusicFile ( true ) for i, v in ipairs ( xmlNodeGetChildren ( file ) ) do table.insert ( data, { tostring ( xmlNodeGetAttribute ( v, "name" ) ), tostring ( xmlNodeGetAttribute ( v, "url" ) ) } ) end xmlUnloadFile ( file ) return data end Link to comment
SpecT Posted April 7, 2015 Author Share Posted April 7, 2015 You can try to follow how I did it in my Nerd Gaming mp3 player script.https://github.com/braydondavis/Nerd-Ga ... client.lua Use xmlLoadFile to load the file, then xmlNodeGetChildren to get all the "song" children, then xmlNodeGetAttribute to get the name, length, and played. This is the function that I used in the NG mp3 player to load the xml radio stations: function getRadioStations ( ) local data = { } local file = xmlLoadFile ( "@xml/usermusic.xml" ) or createUserMusicFile ( true ) for i, v in ipairs ( xmlNodeGetChildren ( file ) ) do table.insert ( data, { tostring ( xmlNodeGetAttribute ( v, "name" ) ), tostring ( xmlNodeGetAttribute ( v, "url" ) ) } ) end xmlUnloadFile ( file ) return data end Oh thanks for the example! I'm gonna try! Link to comment
SpecT Posted April 7, 2015 Author Share Posted April 7, 2015 Yey .. I made it! I have another problem - its about the tables should I keep posting here or to make a new topic for it ? Link to comment
SpecT Posted April 7, 2015 Author Share Posted April 7, 2015 You can post it here imo. Oh hey WhoAmI ... Soo the script is loading the songs in a table (from XML) which is used by the gridlist. Ok ... but I want to get length and played times when I click on the song (For now it loads the song name and artist - get them from the name). I can put length and played times in separate tables but later how could I get the information for the clicked song? BTW the length and played times is storaged in the XML file too. Really bad explained ... hope you will understand Link to comment
WhoAmI Posted April 7, 2015 Share Posted April 7, 2015 If you are using gridlist you can use https://wiki.multitheftauto.com/wiki/Gu ... etItemData Link to comment
SpecT Posted April 7, 2015 Author Share Posted April 7, 2015 If you are using gridlist you can usehttps://wiki.multitheftauto.com/wiki/Gu ... etItemData The problem is that that I'm using dx library which doesn't have that. Link to comment
WhoAmI Posted April 7, 2015 Share Posted April 7, 2015 Well you can insert all values onto table like that table = {} table["Song name"] = times_played_here And after just get data from table using song's name. Link to comment
SpecT Posted April 7, 2015 Author Share Posted April 7, 2015 It's okay for the length but what about the times played? It should be saved somehow ... Link to comment
WhoAmI Posted April 7, 2015 Share Posted April 7, 2015 So make like that table["Song name"] = {length, TimesPlayed} And then get values table["Song name"][1] --length table["Song name"][2] --times played Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 (edited) So make like that table["Song name"] = {length, TimesPlayed} And then get values table["Song name"][1] --length table["Song name"][2] --times played I can't get it ... *facepalm* songs.xml "Feint And Boyinaband - Time Bomb" length="3:32" played="0" /> "Skillet - Rise" length="3:32" played="0" /> "Skillet - Not Gonna Die" length="3:32" played="0" /> So (btw there are more songs) can I get the song info using the song name ? Gridlist (DX): Feint And Boyinaband - Time Bomb Skillet - Rise Skillet - Not Gonna Die When I click on a song it should get it's name (the DX library has that so I already use it to play the song) then get the info about it - length and played. There should be a way to do it ... Here is the XML load part (used Madex code): function getSongs ( ) local songList = { } local file = xmlLoadFile ( "songs.xml" ) for i, v in ipairs ( xmlNodeGetChildren ( file ) ) do table.insert ( songList, { tostring ( xmlNodeGetAttribute ( v, "name" ) ), xmlNodeGetAttribute ( v, "length"), tonumber ( xmlNodeGetAttribute ( v, "played" ) ) } ) end xmlUnloadFile ( file ) return songList end function loadSongsInGridlist() for i, v in ipairs ( getSongs ( ) ) do local name, length, played = unpack ( v ) table.insert(songListGridlist,name) end end addEventHandler("onClientResourceStart",getRootElement(),loadSongsInGridlist) I want to make it like this because right now the script (song lines) is kinda long. For every song I have to set the artist and length ... 665 lines for 120 songs ... guess what would happen when I add more songs Edited April 8, 2015 by Guest Link to comment
WhoAmI Posted April 8, 2015 Share Posted April 8, 2015 I'll be on computer after 3-4 hours. Then I'll do code for you. Try to understand what I want to do. I want to load all items from XML to table like I mentioned before. Then you said that after clicking you get name. If you have name you can get values from table. If you don't understand you have to wait until I'll get on the computer. Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 I'll be on computer after 3-4 hours. Then I'll do code for you. Try to understand what I want to do. I want to load all items from XML to table like I mentioned before. Then you said that after clicking you get name. If you have name you can get values from table. If you don't understand you have to wait until I'll get on the computer. Yey .. that's what I wanted. I'm not in hurry so I will wait you to come. I can't do it because I'm not that "skilled" to work with tables - I know just basic scriting with tables ... Link to comment
ALw7sH Posted April 8, 2015 Share Posted April 8, 2015 function getSongs ( ) local songList = { } local file = xmlLoadFile ( "songs.xml" ) for i, v in ipairs ( xmlNodeGetChildren ( file ) ) do songList[tostring ( xmlNodeGetAttribute ( v, "name" ) )] = {xmlNodeGetAttribute ( v, "length"),tonumber ( xmlNodeGetAttribute ( v, "played" ) )} end xmlUnloadFile ( file ) return songList end function loadSongsInGridlist() for i, v in pairs ( getSongs ( ) ) do local name = i table.insert(songListGridlist,name) end end print(songList["song name"][1]) -- length print(songList["song name"][2]) -- played Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 function getSongs ( ) local songList = { } local file = xmlLoadFile ( "songs.xml" ) for i, v in ipairs ( xmlNodeGetChildren ( file ) ) do songList[tostring ( xmlNodeGetAttribute ( v, "name" ) )] = {xmlNodeGetAttribute ( v, "length"),tonumber ( xmlNodeGetAttribute ( v, "played" ) )} end xmlUnloadFile ( file ) return songList end function loadSongsInGridlist() for i, v in pairs ( getSongs ( ) ) do local name = i table.insert(songListGridlist,name) end end print(songList["song name"][1]) -- length print(songList["song name"][2]) -- played The script bugged at all ... Now the gridlist is empty. Also why in the function loadSongsInGridlist the name = i ? Instead song names it shows numbers ... Link to comment
WhoAmI Posted April 8, 2015 Share Posted April 8, 2015 songList = { } function updateSongs ( ) local file = xmlLoadFile ( "songs.xml" ) for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do local name, length, played = xmlNodeGetAttribute ( v, "name" ), xmlNodeGetAttribute ( v, "length" ), xmlNodeGetAttribute ( v, "played" ) songList[name] = {length, played} end xmlUnloadFile ( file ) end function loadSongsInGridlist() updateSongs ( ) for name, v in pairs ( songList ) do table.insert(songListGridlist,name) end end -- on click event local name = -- get name here local length, played = songList[name][1], songList[name][2] -- on click event Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 songList = { } function updateSongs ( ) local file = xmlLoadFile ( "songs.xml" ) for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do local name, length, played = xmlNodeGetAttribute ( v, "name" ), xmlNodeGetAttribute ( v, "length" ), xmlNodeGetAttribute ( v, "played" ) songList[name] = {length, played} end xmlUnloadFile ( file ) end function loadSongsInGridlist() updateSongs ( ) for name, v in pairs ( songList ) do table.insert(songListGridlist,name) end end -- on click event local name = -- get name here local length, played = songList[name][1], songList[name][2] -- on click event Thanks guys! It worked !! Another question to update the played times I should use xmlNodeSetAttribute ? Link to comment
WhoAmI Posted April 8, 2015 Share Posted April 8, 2015 Yes. Exactly. But remember to save file (xmlSaveFile) after editing. Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 Yes. Exactly. But remember to save file (xmlSaveFile) after editing. Okay, but then I should move the XML load part in server-side. It should edit the file which has to be loaded by everyone. Hm .. I think I will do it. If I have again problems I will write. Thanks again for the help! Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 (edited) Hey. I added a call to the server to update the file then it calls back the client to update the interface. The problem is that that I don't know how to get the song index for xmlFindChild. I should get the song and then it's row and then send it to the server-side to update the value. Edited April 8, 2015 by Guest Link to comment
WhoAmI Posted April 8, 2015 Share Posted April 8, 2015 Get name. Loop for all children's and check if children's name == name. Like that songNameToEdit = "some song"; local file = xmlLoadFile ( "songs.xml" ) for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do if ( xmlNodeGetAttribute ( v, "name" ) == songNameToEdit ) then xmlNodeSetAttribute ( v, "played", xmlNodeGetAttribute ( v, "played" ) + 1 ) end xmlSaveFile ( file ) xmlUnloadFile ( file ) Link to comment
SpecT Posted April 8, 2015 Author Share Posted April 8, 2015 Got lots of erros in debugscript when call it - looks like the count of the songs ... Bad argument @ "xmlNodeGetAttribute" [Expected xml-node at argument 1] Bad argument @ "xmlSaveFile" [Expected xml-node at argument 1] Bad argument @ "xmlUnloadFile" [Expected xml-node at argument 1] 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