Thursday, June 13, 2013

XML Common Root Tag

Have you ever cursed your software vendor for their seemingly endless lack of foresight when it comes to their choice of XML Root Tags? Boy I have... And they always want to charge you big bucks when you desire a necessary correction. As a professional integrator I love reusable code libraries and if you have ever worked with multiple XML documents in a single project you probably have guessed why I say this. Here are a few personal reasons why I like them.
  • Reusing code (and Root Tags) saves programming time, which reduces costs.
  • Sharing code  (and Root Tags) can help prevent bugs, basically the more code a system contains (to process multiple Root Tags)  the more bugs it's likely to have. 
  • Separating code into common libraries lets programmers specialize in their particular strengths. 
Ok, time for me to get off the high horse. Just because our latest ERP project had some poor design work done on their EDI gateway, doesn't mean I need to complain about it. Besides this is a site for posting solutions, so here is my latest. The following script does one thing and only one thing very well. It will take an XML document and give it a new Root Tag, both start and finish.

The script is written in vbScript and it will add a common Root Tag to every XML file it finds in the input directory. Command Line is simple and is easy to script.

Enjoy and feel free to comment.

Stephen


 option explicit  
 Private strRootDir  
 Private strLoadDir  
 Private strOutputDir  
 Private strArchiveDir  
 Private objFSO  
 Private myStr  
 call Main()  
 sub Main  
      if (LoadParams) then  
           set objFSO = CreateObject("Scripting.FileSystemObject")  
           if (not objFSO.FolderExists(strLoadDir)) then objFSO.CreateFolder(strLoadDir)  
           if (not objFSO.FolderExists(strArchiveDir)) then objFSO.CreateFolder(strArchiveDir)  
           call ProcessFiles  
      else  
           DisplayUsage  
      end if  
 end sub  
 private sub DisplayUsage  
  WScript.Echo "Usage: cscript.exe Add_Root.vbs <RootDir> <OutputDir>"  
  WScript.Echo "Example: cscript.exe Add_Root.vbs C:\Users\Stephen\test\Load C:\Users\Stephen\test\Archive"  
  WScript.Echo "Example: cscript.exe Add_Root.vbs C:\Users\Stephen\HDTestInvoices\input C:\Users\Stephen\HDTestInvoices\output"  
 End sub  
 private function LoadParams  
      dim blnResult  
      if (WScript.Arguments.Count = 2) then  
           strRootDir = WScript.Arguments(0)  
           strOutputDir = WScript.Arguments(1)  
           if (right(strRootDir, 1) <> "\") then strRootDir = strRootDir & "\"  
           if (right(strOutputDir, 1) <> "\") then strOutputDir = strOutputDir & "\"  
           strLoadDir = strRootDir  
           strArchiveDir = strOutputDir  
           blnResult = true  
      else  
           blnResult = false  
      end if  
      LoadParams = blnResult  
 end function  
 private sub ProcessFiles  
      dim objSource  
      dim strExt  
      for each objSource in objFSO.GetFolder(strLoadDir).Files  
           strExt = ucase(mid(objSource.Name, InStr(objSource.Name, ".") + 1))  
           if (strExt = "XML") then  
                call ProcessFile(objSource)  
                objSource.Move strArchiveDir  
           end if  
      next  
 end Sub  
 Private sub ProcessFile(objSource)  
      dim objFile  
      dim strLine  
      dim arrFields  
      Dim intIndex  
      Dim blnIsHeader  
      Dim strTranID, strToMail  
      Dim detTranID  
      Dim s1  
      Dim strWorking   
  strWorking = strLoadDir & objSource.Name  
      Set objFile = objFSO.OpenTextFile(strWorking,1)  
      while (not objFile.AtEndOfStream)  
           strLine = objFile.ReadLine  
 '**************************************************************************************************  
           If ((Len(strLine) > 1) and (strLine = "<?xml version=" & """1.0""" & " ?>")) Then  
             strToMail = strLine & "<EDIDOC>"  
             WScript.Echo strLine  
           else  
     strToMail = strToMail & strLine  
     WScript.Echo strLine   
           End If  
 '*****************************************************************************************************  
      Wend  
      strLine = "</EDIDOC>"  
      WScript.Echo strLine  
  strToMail = strToMail & strLine  
  objFile.Close  
      Set objFile = objFSO.OpenTextFile(strWorking,2)  
      objFile.Write(strToMail)  
      objFile.Close  
 end Sub