Jump to content

[SOLVED] HELP! XML ...


SpecT

Recommended Posts

Posted (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 by Guest
  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted

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 

Posted
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!

Posted

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 ?

Posted
You can post it here imo.

:D 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 :oops:

Posted

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.

Posted

So make like that

table["Song name"] = {length, TimesPlayed} 
  

And then get values

table["Song name"][1] --length 
table["Song name"][2] --times played 

Posted (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 by Guest
Posted

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.

Posted
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 ...

Posted
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 

Posted
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 ...

Posted
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 

Posted
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 ?

Posted
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!

Posted (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 by Guest
Posted

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 ) 

Posted

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]

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...