+ Reply to Thread
Results 1 to 6 of 6

Import of multiple XML files

Hybrid View

Gzdnkh Import of multiple XML files 01-16-2012, 07:46 AM
Chippy Re: Import of multiple XML... 01-16-2012, 03:30 PM
MaczaQ Re: Import of multiple XML... 01-17-2012, 03:22 PM
Gzdnkh Re: Import of multiple XML... 01-18-2012, 02:45 AM
MaczaQ Re: Import of multiple XML... 01-18-2012, 07:51 AM
Gzdnkh Re: Import of multiple XML... 01-23-2012, 02:57 AM
  1. #1
    Registered User
    Join Date
    01-03-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    29

    Import of multiple XML files

    Hello,
    I have a problem with an each day task:
    1. Download from a set ftp a file (self extracting .exe) with set name to a set folder on my hard drive.
    2. Extract the file to get a varying number (I estimate 10 - 1000 separate files) of .xml files. All the files have the same schema/mapping.
    3. Check the same "tag" in each file against a set table of values (under 100 different values) -> find the files with desired values.
    4. Present them in a usable format.

    I was advised that all the points can be automated in Excel.
    Doing 1 and 2 manually I can live with so I started with point 3.
    As I have no experience with VBA and only a superficial grasp of Excel and programming I got stuck.

    A. First of all I tried to use (Developer -> XML -> Import) to import all the files at once (Idea from built in help)
    It does not work. I get "XML file importing error" window which lists all the files with either:
    "Not able to find XML map corresponding to this data"
    or "No data was imported or only a part of data was imported" <- no data in a workbook

    I tried to pre-map (dragged the map from the source) the workbook. It works fine for one file only or for the first file of many.

    I left it at that.

    B. I used (Data -> External Data -> Other sources -> from xml data) to get the files into a workbook.
    I recorded a macro to find out that I need to use
    ActiveWorkbook.XmlImport URL:= _
            "C:\FolderWithFilesToBeImported\*.xml" _
            , ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$1")
    I just have to feed the names of the files to the function in a loop.(?)

    C. I wrote some code to check what names the files from directory have and to put them in an Array
    Sub Import()
        Dim FileNames() As String
        Dim i As Integer
        
        Do
            ReDim Preserve FileNames(i)
            FileNames(i) = Dir("C:\FolderWithFilesToBeImported\*.xml")
            i = i + 1
        Loop Until FileNames(i - 1) = ""
    End Sub
    It fills the array with the name of the first file in the folder an infinite number of times.
    As the help for "Dir" suggests I tried running Dir("") to get to the next file but with no success.

    Please excuse the length of the post but I do not know if any of the above is a good idea.
    Moreover my Excel is in Polish so I can only hope that I translated correctly.

    I would greatly appreciate any hints, advises or solutions.

    I have posted the same on another forum, but it is there for over a week with no replies, so I think I am in clear to post here.

    Edit:
    Sample XML files. I was not able to attach them as .xml so I cheated a bit.
    266355_2011.xml.xls
    266357_2011.xml.xls
    266359_2011.xml.xls
    Last edited by Gzdnkh; 01-23-2012 at 02:58 AM. Reason: uploading files

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Import of multiple XML files

    This should get you started.
    Option Explicit
    
    Public Sub Import_XML_Files()
    
        Dim folder As String
        Dim filename As String
        Dim dest As Range
        
        folder = "C:\FolderWithFilesToBeImported\"          'CHANGE THIS TO SUIT
         
        'Sheet and cell where imported XML data will start
        
        Set dest = Sheets("Sheet1").Range("A1")
        
        'Clear cells and delete all XML maps
        
        dest.Parent.Cells.ClearContents
        While ActiveWorkbook.XmlMaps.Count > 0
            ActiveWorkbook.XmlMaps(ActiveWorkbook.XmlMaps.Count).Delete
        Wend
        
        If Right(folder, 1) <> "\" Then folder = folder & "\"
        filename = Dir(folder & "*.XML")
        
        Do While filename <> ""
            
            If ActiveWorkbook.XmlMaps.Count = 0 Then
                'Import first XML data file, creating a new XML mapping
                ActiveWorkbook.XmlImport URL:=folder & filename, ImportMap:=Nothing, Overwrite:=True, Destination:=dest
            Else
                'Append subsequent XML data files to existing mapping - don't specify Destination
                ActiveWorkbook.XmlImport URL:=folder & filename, ImportMap:=Nothing, Overwrite:=False
            End If
            
            'Get next matching file
            filename = Dir
            
        Loop
    
    End Sub

  3. #3
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Import of multiple XML files

    hi,

    I have some other way a bit similar to presented by Chippy
    Sub MultiXMLimport()
    PathToFiles = "C:\PathToYourXMLfiles\"
    Application.DisplayAlerts = False
    Cells.Clear
    
    For Each XML_map In ThisWorkbook.XmlMaps
        XML_map.Delete
    Next
    
        For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(PathToFiles).Files
            If LCase(Right(file, 4)) = ".xml" Then
                If ThisWorkbook.XmlMaps.Count = 0 Then
                  ActiveWorkbook.XmlImport URL:=file, ImportMap:=Nothing, Overwrite:=False, Destination:=Range("A1")
                Else
                  ActiveWorkbook.XmlImport URL:=file, ImportMap:=Nothing, Overwrite:=False
                End If
            End If
        Next file
    Application.DisplayAlerts = True
    End Sub
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

  4. #4
    Registered User
    Join Date
    01-03-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Import of multiple XML files

    Hello, thank you for the help but I am in need of some more.

    Running both the codes imports the first file and the first map and then I get a:

    Run-time error '-2147467259 (90004005)'
    "Błąd importu. Nie zaimportowano żadnych danych" which translates to "Import error. No data was imported" i guess.

    I have no idea what is wrong, but I tried this (Chippy's code as base as it is more understandable for me with bits from maczaq)...:
    Option Explicit
    
    Public Sub ImportPossiblyWorking()
    
        Dim folder As String
        Dim filename As String
        Dim dest As Range
        Dim i As Integer
        
        Cells.Clear
        folder = "C:\FilesToBeImported\"          'CHANGE THIS TO SUIT
         
        While ActiveWorkbook.XmlMaps.Count > 0
            ActiveWorkbook.XmlMaps(ActiveWorkbook.XmlMaps.Count).Delete
        Wend
        
        If Right(folder, 1) <> "\" Then folder = folder & "\"
        filename = Dir(folder & "*.XML")
        
        Do While filename <> ""
            i = i + 1
            ActiveWorkbook.XmlImport URL:=folder & filename, ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$" & i)
            filename = Dir
        Loop
    
    End Sub
    ...and it worked, all the files got imported.
    I just do not know if there are any "holes" in the code. Are there any?

  5. #5
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Import of multiple XML files

    Hello Gzdnkh,

    Could you attach any source XML file. I could try to test it (wgraj pliki z danymi do testu ;-)

  6. #6
    Registered User
    Join Date
    01-03-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Import of multiple XML files

    Hello,
    Thank you for all the help, now I have something to work with.
    So far I have discovered that every answer leads to many questions

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1