| Home | News | Products | Buy online | Solutions | Contact us | |
| Sitemap | About | Download | Support | Services | Feedback |
How to generate an RSS file from a directory containing XML files.
Create the following files and shortcuts in C:\GenRSS\:
GenerateRSSSubdirs.cmd
@ECHO OFF @ECHO. @ECHO Generating RSS feed for current directory: CD @ECHO. CScript //nologo C:\GenRSS\GenerateRSS.vbs %1 %2 %3 %4 %5 %6 %7 %8 %9 /S REM PAUSE
GenerateRSS.cmd
@ECHO OFF @ECHO. @ECHO Generating RSS feed for current directory: CD @ECHO. CScript //nologo C:\GenRSS\GenerateRSS.vbs %1 %2 %3 %4 %5 %6 %7 %8 %9 PAUSE
GenerateRSS.vbs
' Copyright notice - do not remove
WScript.Echo "Generate RSS v1.0 by Spinner Software"
WScript.Echo "Visit us at http://www.spinnersoftware.com/"
WScript.Echo ""
Function AddFilesInFolder(fso, FolderName, lFileMask, lBaseURL, lOutputExtension, lTitleField, lDescriptionField, IncludeSubfolders)
Dim lFolder, lColl, lFile, lDir, lXML, lFName
lXML = ""
Set lFolder = fso.GetFolder(FolderName)
Set lColl = lFolder.Files
For Each lFile in lColl
lCurrentFile = lFile.Name
lExt = fso.GetExtensionName(lCurrentFile)
WScript.Echo "Found file " & lCurrentFile & " (Extension is " & lExt & ")"
If (StrComp(lExt,"xml",vbTextCompare) = 0) Then
WScript.Echo " Checking file for prefix " & lFileMask
If (StrComp(Left(lCurrentFile, Len(lFileMask)), lFileMask, vbTextCompare) = 0) Then
WScript.Echo " Reading file " & lCurrentFile
Dim lTitle, lDescription, lXMLEntry
If(GetFileEntries(fso, FolderName & "\" & lCurrentFile, lTitleField, lDescriptionField, lTitle, lDescription)) Then
WScript.Echo " Title=" & lTitle & ", Description=" & lDescription
lFName = lBaseURL & fso.GetBaseName(lCurrentFile) & "." & lOutputExtension
lXMLEntry = GetItemXML(lTitle, lFName, lDescription)
'WScript.Echo "XML = " & lXMLEntry
lXML = lXML & lXMLEntry
End If
End If
End If
Next
If IncludeSubfolders Then
Set lColl = lFolder.SubFolders
For Each lDir in lColl
WScript.Echo "Checking subdir " & lDir
lXML = lXML & AddFilesInFolder(fso, FolderName & "\" & lDir.Name, lFileMask, lBaseURL & lDir.Name & "/", lOutputExtension, lTitleField, lDescriptionField, IncludeSubfolders)
Next
End If
Set lColl = Nothing
Set lFolder = Nothing
AddFilesInFolder = lXML
End Function
Function GetFileEntries(fso, File, TitleField, DescriptionField, TitleValue, DescriptionValue)
Dim xmlDoc, lNode
TitleValue = ""
DescriptionValue = ""
GetFileEntries = False
Set xmlDoc = CreateObject("MSXML2.DOMDocument.4.0")
If Not xmlDoc Is Nothing Then
xmlDoc.async = False
WScript.Echo " Loading File " & File
xmlDoc.load File
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set lNode = xmlDoc.selectSingleNode("//" & TitleField)
TitleValue = lNode.text
Set lnode = xmlDoc.selectSingleNode("//" & DescriptionField)
DescriptionValue = lNode.text
Set lNode = Nothing
Set xmlDoc = Nothing
GetFileEntries = True
End if
End Function
Function GetItemXML(lTitle, lLink, lDescription)
GetItemXML = " <item>" & vbCrLf & _
" <title>" & lTitle & "</title>" & vbCrLf & _
" <link>" & lLink & "</link>" & vbCrLf & _
" <description>" & lDescription & "</description>" & vbCrLf & _
" </item>" & vbCrLf
End Function
Function WriteToTextFile(fso, Filename, TextData)
Dim lOutputFile
Set lOutputFile = fso.OpenTextFile(Filename, 2, True, 0)
lOutputFile.Write TextData
lOutputFile.Close
Set lOutputFile = Nothing
WriteToTextFile = True
End Function
'
'
' Check for command line parameters
'
'
Dim objArgs, lParmIndex
Dim lFileMask, lBaseURL, lTitleField, lDescriptionField
Dim lOUtputFile, lChTitle, lChDescription, lChLanguage, lOutputExtension
Dim lCurrentFile, lExt, lInclSubfolders
Set objArgs = WScript.Arguments
If objArgs.Count < 9 Then
WScript.Echo "Not enough parameters specified."
WScript.Echo "Syntax: GenerateRSS OutputFile Fileprefix BaseURL ChTitle ChDescription ChLanguage OutputExtension TitleField DescriptionField [/S]"
Else
WScript.Echo "Command line parameters: "
For lParmIndex = 0 To objArgs.count - 1
WScript.Echo " Parameter " & lParmIndex & " = " & objArgs(lParmIndex)
Next
' Set parameters
lOutputFile = objArgs(0)
lFileMask = objArgs(1)
If(lFileMask = "*") Then lFileMask = ""
lBaseURL = objArgs(2)
lChTitle = objArgs(3)
lChDescription = objArgs(4)
lChLanguage = objArgs(5)
lOutputExtension = objArgs(6)
lTitleField = objArgs(7)
lDescriptionField = objArgs(8)
lInclSubfolders = False
If objArgs.count = 10 Then
If(StrComp(objargs(9), "/S", vbTextCompare) = 0) Then
lInclSubfolders = True
WScript.Echo "Including subfolders."
End If
End If
Dim fso, wSh, lFolder, lXML
Set fso = CreateObject("Scripting.FileSystemObject")
Set wSh = CreateObject("WScript.Shell")
lFolder = wSh.CurrentDirectory
lXML = AddFilesInFolder(fso, lFolder, lFileMask, lBaseURL, lOutputExtension, lTitleField, lDescriptionField, lInclSubfolders)
lXML = "<rss version=""0.91"">" & vbCrLf & _
" <channel>" & vbCrLf & _
" <title>" & lChTitle & "</title>" & VbCrLf & _
" <link>" & lBaseURL & "</link>" & VbCrLf & _
" <description>" & lChDescription & "</description>" & VbCrLf & _
" <language>" & lChLanguage & "</language>" & vbCrLf & _
lXML & _
" </channel>" & VbCrLf & _
"</rss>"
' Write to output file
WriteToTextFile fso, lFolder & "\" & lOutputFile, lXML
Set wSh = Nothing
Set fso = Nothing
End If
WScript.Echo "Quitting."
WScript.Quit
Script explanation
The command line syntax for the .vbs file is:
GenerateRSS OutputFile Fileprefix BaseURL ChTitle ChDescription ChLanguage OutputExtension TitleField DescriptionField [/S]
The vbs script runs through the target folders/files and tests the filename against the fileprefix. If a "*" is used, all XML files that are found are added to the RSS output.
The XML file is then read, and the two node/entry values are read. These form the Item Title and Description fields in the output RSS. The Item Link entry is calculated based on the baseurl provided and the subfolder\filename of the XML file that was found.
The ChannelTitle, ChannelDescription, and language (en-us) are added to the channel information in the output RSS.
The OutputExtension is typically "html". This means all the calculated links points to an .html file instead of pointing to the .xml file.
Creating shortcuts
You can now create shortcuts to generate the RSS feed:
Target: C:\GenRSS\GenerateRSS.cmd Outputfile.rss * http://www.mysite.com/myfolder/ ChannelTitle ChannelDescription en-us html node/entry node/entry Working directory: C:\Intranet\MySite\MyDirectory\
Change your shortcuts to have a working directory that points to the directory containing the XML files you want the RSS feed for. Make sure the target points to the proper cmd file, you can use either GenerateRSS or GenerateRSSSubdirs depending on your need. The two command files are identical except that GenerateRSSSubdirs also passes a /S parameter to the vbs script.
The output RSS file
The resulting RSS file is valid XML that looks like this:
<rss version="0.91">
<channel>
<title>ChannelTitle</title>
<link>http://www.mysite.com/myfolder/</link>
<description>ChannelDescription</description>
<language>en-us</language>
<item>
<title>Read from node in myxmlfile</title>
<link>http://www.mysite.com/myfolder/myxmlfile.html</link>
<description>Read from node in myxmlfile</description>
</item>
</channel>
</rss>
You can then render data from the RSS file in an XML/XSL file, as described here.
--------------------------------------------------------------------------------
(c) 2004 Nicolai Kjaer, Spinner Software B.V.
http://www.spinnersoftware.com
Copyright 1997-2004 Spinner Software BV (www.spinnersoftware.com).
Home | Products | Buy Online | Privacy statement | Price policy | About Spinner | Contact Us | Search
This page is fully HTML 4.01 and CSS compliant. Please email the webmaster if you encounter any problems with this site.