PDA

View Full Version : Stuck in XML


playmenow
03-17-2006, 05:50 AM
For the first time, I'm stuck in XML. I have a long code to save a playlist. I attached it, because is too big. For some reason, I get an error...Failed to insert XML or something like that (I used the debug windows). I also attached the project, it may help. Thanks :yes

playmenow
03-17-2006, 02:03 PM
I corrected that error, but I found 2 more errors with XML. First, the LoadPlaylist function wil add only 1 item in the playlist (xml part). Second, SavePlaylist will only save the last file in the ListBox (xml part). Any ideas? :huh


function LoadPlaylist()

-- ask for the path of the playlist and load it
FilePath = Dialog.FileBrowse(true, "Locate File", _DesktopFolder, "M3U Playlist|*.m3u|XML Playlist|*.xml|", nil, nil, false, true);
-- make sure that cancel was not pressed
if FilePath[1] ~= "CANCEL" then
-- find the extension
ext = String.SplitPath(FilePath[1]).Extension;
-- if it's m3u, it's a text file
if ext == ".m3u" then
-- read the contents
play_file = TextFile.ReadToTable(FilePath[1]);
-- make sure it's not empty
if play_file ~= nil then
for j,Path in play_file do
-- get the file name and add it to the playlist
file_name = String.SplitPath(Path).Filename;
ListBox.AddItem("Playlist", file_name, Path);
end
end
-- if it's xml, it's a complex file
elseif ext == ".xml" then
-- something needed
if i == nil then
i = 0
end
-- load the xml
XML.Load(FilePath[1]);
-- get the value
j = i+1
i = j
data = XML.GetValue("*/file"..j);
-- make sure it's not empty
if data ~= nil then
-- get the file name and add it to the playlist
file_name = String.SplitPath(data).Filename;
ListBox.AddItem("Playlist", file_name, data);
end
end
end
end

-- ******** Another funtion ********

function SavePlaylist()

-- ask for the folder
PlayListFolder = Dialog.FolderBrowse("Please select the folder where your M3U playlist will be saved", _DesktopFolder);
if PlayListFolder ~= "CANCEL" then
-- ask what type of playlist
type = Dialog.Message("XML?", "Do you want the playlist to be XML or M3U? For XML press YES, for M3U press NO.", MB_YESNO, MB_ICONQUESTION, MB_DEFBUTTON1);
if type ~= 6 then
-- do what's needed
FolderFound = Folder.DoesExist(PlayListFolder);
PlayList = nil;
Files = ListBox.GetCount("Playlist");
if Files == 0 then
-- say if the playlist is empty
Dialog.Message("Notice", "The playlist is empty.", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
else
-- ask for the file name
PlayList = Dialog.Input("Please enter the file name.", "File Name:", nil, MB_ICONQUESTION);
PlayListFound = File.DoesExist(PlayListFolder.."\\"..PlayList..".m3u");
if PlayListFound == true then
-- say if the playlist can be overwritten
proceed = Dialog.Message("Notice", "The playlist is not empty. Do you want to overwrite?", MB_YESNO, MB_ICONINFORMATION, MB_DEFBUTTON1);
if proceed == IDYES then
Out = nil;
TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, false);
for i=1,Files do
Data = ListBox.GetItemData("Playlist", i);
Out = Data.."\r\n";
TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, true);
end
elseif proceed == IDNO then
PlayList = nil;
-- ask if save with another name or no
note = Dialog.Message("Notice", "Do you want to proceed with another file name?", MB_YESNO, MB_ICONINFORMATION, MB_DEFBUTTON1);
if note == IDYES then
-- say if the file already exists
PlayList = Dialog.Input("File exists", "Please choose another file name :", nil, MB_ICONQUESTION);
for i=1,Files do
Data = ListBox.GetItemData("Playlist", i);
Out = Data.."\r\n";
TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, true);
end
elseif note == IDNO then
end
end
else
for i=1,Files do
Data = ListBox.GetItemData("Playlist", i);
Out = Data.."\r\n";
TextFile.WriteFromString(PlayListFolder.."\\"..PlayList..".m3u", Out, true);
end
end
end
elseif type == 6 then
-- do what's needed
FolderFound = Folder.DoesExist(PlayListFolder);
PlayList = nil;
Files = ListBox.GetCount("Playlist");
if Files == 0 then
-- say if the playlist is empty
Dialog.Message("Notice", "The playlist is empty.", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
else
-- ask for the file name
PlayList = Dialog.Input("Please enter the file name.", "File Name:", nil, MB_ICONQUESTION);
PlayListFound = File.DoesExist(PlayListFolder.."\\"..PlayList..".xml");
if PlayListFound == true then
-- say if the playlist can be overwritten
proceed = Dialog.Message("Notice", "The playlist is not empty. Do you want to overwrite?", MB_YESNO, MB_ICONINFORMATION, MB_DEFBUTTON1);
if proceed == IDYES then
-- do what's needed
for i=1,Files do
File.Copy("AutoPlay\\Docs\\playlist.xml", PlayListFolder.."\\"..PlayList..".xml", false, true, true, true, nil);
XML.Load(PlayListFolder.."\\"..PlayList..".xml");
Data = ListBox.GetItemData("Playlist", i);
XML.InsertXML("*/note", "<file"..i..">"..Data.."</file"..i..">\r\n\r\n", XML.INSERT_AFTER);
XML.Save(PlayListFolder.."\\"..PlayList..".xml");
end
elseif proceed == IDNO then
PlayList = nil;
-- ask if save with another name or no
note = Dialog.Message("Notice", "Do you want to proceed with another file name?", MB_YESNO, MB_ICONINFORMATION, MB_DEFBUTTON1);
if note == IDYES then

-- ask for another file name
PlayList = Dialog.Input("File exists", "Please choose another file name :", nil, MB_ICONQUESTION);
-- do what's needed
for i=1,Files do
File.Copy("AutoPlay\\Docs\\playlist.xml", PlayListFolder.."\\"..PlayList..".xml", false, true, true, true, nil);
XML.Load(PlayListFolder.."\\"..PlayList..".xml");
Data = ListBox.GetItemData("Playlist", i);
XML.InsertXML("*/note", "<file"..i..">"..Data.."</file"..i..">\r\n\r\n", XML.INSERT_AFTER);
XML.Save(PlayListFolder.."\\"..PlayList..".xml");
end
end
end
else
-- do what's needed
for i=1,Files do
File.Copy("AutoPlay\\Docs\\playlist.xml", PlayListFolder.."\\"..PlayList..".xml", false, true, true, true, nil);
XML.Load(PlayListFolder.."\\"..PlayList..".xml");
Data = ListBox.GetItemData("Playlist", i);
XML.InsertXML("*/note", "<file"..i..">"..Data.."</file"..i..">\r\n\r\n", XML.INSERT_AFTER);
XML.Save(PlayListFolder.."\\"..PlayList..".xml");
end
end
end
end
end
end


Yes, I know, the code is a mess. :rolleyes