Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

...I enjoy spending time on it for personal growth (I learn from the questions I don't answer, and I reinforce/stay sharp on the topics I do answer), and to give back to humanity at large...

Geography

Where in the world do Tek-Tips members come from?

Use VB to create Excel file from multiple XML files

techadmin2012 (TechnicalUser)
27 Apr 12 18:01
I have over 800 XML files in a directory. I need to read each file, extract 2 pieces of data from each and populate an excel spread sheet. One column is the first piece of data and column 2 is the second piece of data. Each XML file is only one line long.

Can anyone help me with this?
xwb (Programmer)
28 Apr 12 12:59
It will be something like this.  Say your xml file looks like

CODE

<members>
  <member>
    <connectedto name="upper arm">body</connectedto>
  </member>
</members>
I know you said your xml files are only one line long - it doesn't really matter as xml only has new lines for readability

The code will look something like this.  The main problem is the pathnames.  If you use relative pathnames, excel's is relative to My Documents, FSO and WShell are relative to the current directory.

CODE

' This needs to be the full filepath.
const excelFilename = "%AllUsersProfile%\Documents\xlextract\collate.xls"
' This also needs to be the full path.
const xmlRepository = "%AllUsersProfile%\Documents\xlextract"

' Get all the things we need
set objFSO = CreateObject ("Scripting.FileSystemObject")
set objExcel = CreateObject ("Excel.Application")
set objXML = CreateObject ("MSXML2.DOMDocument.6.0")
set objDOS = CreateObject ("WScript.Shell")

if isNull(g_objXML ) then
   ' Try version 5.  This is standard for XP
   set objXML  = CreateObject ("MSXML2.DOMDocument.5.0")
end if

' Create an excel spreadsheet
objExcel.Workbooks.Add
objExcel.Cells(1,1) = "Name"
objExcel.Cells(1,2) = "Connected To"

' Get all the XML files in the current directory
xmlRepositoryFull = objDOS.ExpandEnvironmentStrings(xmlRepository)
WScript.echo "Examining files in " & xmlRepositoryFull
set objDir = objFSO.GetFolder(xmlRepositoryFull)
objXML.validateOnParse = true
objXML.async = false
row = 1
for each file in objDir.Files
   if right(file.name, 4) = ".xml" then
      WScript.echo file.name
      objXML.load file.name
      set taglist = objXML.getElementsByTagName ("connectedto")
      for each tag in taglist
         name = tag.getAttribute ("name")
         connect = tag.text
         WScript.echo name + "=" + connect         
         row = row + 1
         objExcel.Cells(row,1) = name
         objExcel.Cells(row,2) = connect
      next
   end if   
next

excelFilenameFull = objDOS.ExpandEnvironmentStrings(excelFilename)
on error resume next
WScript.Echo "Deleting " & excelFilenameFull & " if it exists"
objFSO.DeleteFile excelFilenameFull
WScript.Echo "Saving as " & excelFilenameFull
objExcel.ActiveWorkbook.SaveAs excelFilenameFull
objExcel.ActiveWorkbook.Close
objExcel.Quit

WScript.Quit
If it falls over, remember to go into task manager and delete the excel process that the script created.  If you don't you could end up with a lot of excel processes in the background.
techadmin2012 (TechnicalUser)
1 May 12 14:54


Thanks for the help. This partially works. Here is what I have in each of my XML files. I need create a spreadsheet with headings for Invoice Response Number
Original Invoince Number
Status
Invoice Response reason Comments.

I need each column to then be populated from the data in each of these. I can't get the data to pull in. I have created the spreadsheet, added column headers and I can see that it is reading each file, but not pulling in the data.

- <ns0:InvoiceResponse xmlns:ns0="http://Cortex.TotalSafety.TotalSafetyInvoiceResponse">
- <Header>
  <Invoice_Response_Number>453640</Invoice_Response_Number>
  <Original_Invoice_Number>453640</Original_Invoice_Number>
  <Status_Code>RECEIVED</Status_Code>
  </Header>
  </ns0:InvoiceResponse>
techadmin2012 (TechnicalUser)
1 May 12 19:53
I found a solution and it is pretty simple. Simply create a macro:

Sub xmlImport()

    fld = "c:\cortex\inbound\"
    i = 2
    found = False
    fil = Dir(fld & "*.xml")
    Do While (fil <> "")
        found = False
        Location = fld & fil
        ActiveWorkbook.xmlImport URL:=Location, ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A" & i)
        Do
            If Cells(i, 2) = "" Then
                found = True
            Else
                i = i + 1
            End If
        Loop Until found
        fil = Dir
    Loop
End Sub
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close