Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Create XML database of file listing

  • Filter
  • Time
  • Show
Clear All
new posts

  • PBCC Create XML database of file listing

    I have a lot of mp3's - and I want to display them on my web page so I need to either use ASP (I've done that) or create an XML database of my MP3 listing...

    To display XML in a friendly format I am using XSL markup language - and I've included that as well.
    But the main point here is that this executable will create an XML database of all of your mp3's (Or other files)

    This is in it's infancy right now and I will be posting updates.

    USEAGE: XMLDrive <path to your mp3's> <output dir where the XML will go> <file extension - usually mp3>

    Filename *must* be in artist - songname.mp3 format
    XML does not allow certain special characters - I will be adding filters in when I get a filter list from work (We do XML and XSL)...

    #Option Version5
    #Register None
    #Include "WIN32API.INC"
    Declare Function WriteDTDFile(OutputDir As String,filename As String) As Long
    Function WinMain (ByVal hInstance     As Long, _
                      ByVal hPrevInstance As Long, _
                      ByVal lpCmdLine           As Asciiz Ptr, _
                      ByVal iCmdShow      As Long) As Long
    Local hFile         As Long
    Local lResult       As Long
    Local lCount        As Long
    Local lLoop         As Long
    Local inputdir      As String
    Local outputdir     As String
    Local FileName      As String 'baseline of filename as directed by user
    Local sTmp          As String
    Local buff          As String
    Local fd          As WIN32_FIND_DATA
    Local sSearchSpec As String
    Local ncPaths     As Long
    Local hFind       As Long
    Local qcbFileSize As Quad
    Dim asPathList(1 To 1) As String
    If IsFalse Len(Command$) Then
        Color 11,0
        StdOut "xmlDrive ";
        Color 14,0
        StdOut "<inputdir> <outputdir> <fileextension>"
        Color 7,0
        StdOut "<inputdir> is the directory scanned"
        StdOut "<outputdir> is the folder the dtd and xml file are written to"
        StdOut "<filename> is the name assigned to .xml and .dtd (ie mp3.dtd, mp3.xml)"
    End If
    inputdir    = Parse$(Command$," ",1)
    outputdir   = Parse$(Command$," ",2)
    filename    = Parse$(Command$," ",-1)
    If filename = outputdir Then 'filename left off
       StdOut "Filename not specified"
       Exit Function
    End If
    If Right$(InputDir,1) <> "\" Then InputDir = InputDir & "\"
    If Right$(outputDir,1) <> "\" Then outputDir = outputDir & "\"
    lResult = WriteDTDFile(outputdir,filename)
    hFile = FreeFile
    Open outputdir & filename & ".xml" For Output As #hFile
    Print #hFile, "<?xml version=""1.0""?>"
    Print #hFile, "<!DOCTYPE mp3list SYSTEM """ & filename & ".dtd"">"
    Print #hFile, "<?xml-stylesheet type=""text/xsl"" href=""" & filename & ".xsl""?>
    Print #hFile, "<mp3list>"
    'Begin scanning drive
    sSearchSpec = "*." & Filename
    ' Treat any periods as literals rather than as regex codes.
    Replace "." With "\." In sSearchSpec
    ncPaths = 1
    asPathList(ncPaths) = inputdir
    ' Scan for matching files, one directory at a time.
        hFind = FindFirstFile(asPathList(1) + "*", fd)
        If hFind <> Bits&(%INVALID_HANDLE_VALUE) Then
                If fd.cFilename <> "." And fd.cFilename <> ".." Then
                    ' If it's a directory, add it to the search list.
                    If (fd.dwFileAttributes And %FILE_ATTRIBUTE_DIRECTORY) Then
                        Incr ncPaths
                        ReDim Preserve asPathList(1 To ncPaths) As String
                        asPathList(ncPaths) = asPathList(1) + fd.cFilename + "\"
                    End If
                    ' If it's a matching file, display it.
                    Incr lCount
    '                StdOut asPathList(1) + fd.cFilename
                    'XML Engine - parse and write the elements here
                    Print #hFile, "<" & filename & " id=""" & Format$(lCount) & """>"  'Open tag for element mp3
                    Print #hFile, String$(5,32) & "<artist>";
                    Print #hFile, Trim$(Parse$(fd.cFilename,"-",1));
                    Print #hFile, "</artist>"
                    Print #hFile, String$(5,32) & "<title>";
                    sTmp = Trim$(Parse$(fd.cFilename,"-",-1))
                    If Right$(sTmp,4) = "." & filename Then sTmp = Left$(sTmp,Len(sTmp)-4)
                    Print #hFile, sTmp;
                    Print #hFile, "</title>"
                    Print #hFile, String$(5,32) & "<path>";
                    sTmp = asPathList(1)
                    If Right$(sTmp,1) = "\" Then sTmp = Left$(sTmp,Len(sTmp)-1)
                    Print #hFile, sTmp;
                    Print #hFile, "</path>"
                    Print #hFile, "</" & filename & ">"  'Open tag for element mp3
                End If
             Loop While FindNextFile(hFind, fd)
             FindClose hFind
        End If
        Array Delete asPathList()
        Decr ncPaths
    Loop While ncPaths
    StdOut Format$(lCount) & " files.."
    Print #hFile, "</mp3list>"
    Close #hFile
    End Function
    Function WriteDTDFile(OutputDir As String,filename As String) As Long
    Local hFile As Long
    hFile = FreeFile
    Open OutPutDIr & filename & ".dtd" For Output As #hFile
    Print #hFile,"<?xml version=""1.0""?>"
    Print #hFile, "<!ELEMENT mp3list (mp3,author,title,path)>"
    Print #hFile, "<!ELEMENT mp3 (#PCDATA)>"
    Print #hFile, "<!ELEMENT artist (#PCDATA)>"
    Print #hFile, "<!ELEMENT title (#PCDATA)>"
    Print #hFile, "<!ELEMENT path (#PCDATA)>"
    Close #hFile
    Function = %TRUE
    End Function

    Now to make it fancy you will want the XSL otherwise it will display in ugly XML format, XSL takes XML input and outputs it as HTML

    Modify as you see fit

    'Filename must match the extension name, ie mp3.xsl
    <?xml version='1.0' encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="">
        <xsl:template match="/">
                    <title>MP3 Collection</title>
    <style type="text/css">
    .warn  {color: black;text-transform:none;font-style:normal;font-weight:bolder;background:yellow;}
    .del {color: black;text-decoration:Line-through;background:#F66;}
    .ins{color: black;text-decoration:None;background:yellow;}
    A:link{text-decoration: None; Color: #000080;}
    A:visited {text-decoration: None; Color: #000080;}
    A:active{text-decoration:None; Color: #000080;}
    A:hover{text-decoration: None; Color: #FF4040;background: #000080;}
    <body bgcolor="#000000" text="#000000">
    <div align="center">
      <table border="0" width="655" height="142">
          <td width="150" height="142">
    		<img src="" width="125" height="82"></img>
          <td width="990" height="142">
    		<p algin="center" align="center">
    			<font size="7"><font color="#FF0000">T</font><font color="#0000FF">he</font> <font color="#FF0000">N</font><font color="#0000FF">ight
    			</font><font color="#FF0000">G</font><font color="#0000FF">allery </font></font><font color="#FF0000" size="7">BBS
    			<font size="4" color="#FFFFFF"><b>Since July 1994</b></font>
    <table border="2" bordercolor="black" BGCOLOR="#7191F7" align="center">
    	<tr BGCOLOR="#7191F7">
    		<table border="0" width="100%">
    				<a href=""> <img  border="0" src=""></img></a>
    			<td>TNGMP3! v3.0 <br />
    			<span class="warn">Copyright (c) 2002-2005</span><br />
    			<a href="">Computer Creations Software</a>
    	        <td bgcolor="#8080DD"><Font color="#FF0000">Path</Font></td>
    	        <td bgcolor="#8080DD"><Font color="#FF0000">Artist</Font></td>
    	        <td bgcolor="#8080DD"><Font color="#FF0000">Title</Font></td>
    	<xsl:for-each select="mp3list/mp3">
    	<tr BGCOLOR="#7191F7">
    	    <td ALIGN="left" bgcolor="#8080DD" color="#000080"><font face="Tahoma,Verdana" size="2" color="#000080"><xsl:value-of select="path"/></font></td>
    	    <td ALIGN="left" bgcolor="#8080DD" color="#000080"><font face="Tahoma,Verdana" size="2" color="#000080"><xsl:value-of select="artist"/></font></td>
    	    <td ALIGN="left" bgcolor="#8080DD" color="#000080"><font face="Tahoma,Verdana" size="2" color="#000080"><xsl:value-of select="title"/></font></td>
    Scott Turchin
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi