0

I'm rather a newbie in XML, but I have a pretty good experience in VBA-access. We need create an XML file to send daily overnight actual driver information of about 1500 cars

Searched for hours always ended on a dead end. What would be the best approach? DOM?, PLAIN TEXT? Love you get some direction here.

Many thanks in advance

The receiver wants it in this format:

<?xml version="1.0" encoding="UTF-8"?>
<REGDriverInformationData xsi:schemaLocation="http://www.Provider.fr/REGDriverInformation     REGDriverInformation.xsd " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.Provider.fr/REGDriverInformation">
<Header>
     <SequenceNo>1</SequenceNo>
     <RsNumber>123456</RsNumber>
</Header>
<REGDriverInformation>
     <LicensePlate>DE125665</LicensePlate>
  <Driver>
    <FirstName>Driver1_FirstName</FirstName>
    <LastName>Driver1_LastName</LastName>
    <BirthDate>1988-10-12</BirthDate>
    <Address>
       <Street>Driver1_Street</Street>
       <HouseNumber>Driver1_HouseNumber</HouseNumber>
       <PostalCode>Driver1_PostalCode</PostalCode>
       <City>Driver1_City</City>  
       <CountryCode>UK</CountryCode>
     </Address>
  <DrivingLicenseNumber>Driver1_DrivingLicenseNumber</DrivingLicenseNumber>
</Driver>
<Start>2014-05-01T00:00:00</Start>
<End>2018-04-30T00:00:00</End>
</REGDriverInformation>
</REGDriverInformationData>

3 Answers 3

2

As suggested, you want to use the MSXML file. Your problem is you (unfortunately) have name spaces, and 99% of basic (and clean) VBA examples don’t include the name space. In a simple nut shell, name spaces are the cause of world poverty, expensive software, and a general suffering in the software industry. You also don’t state if you have to create a separate xml file for each record, or you want the data in a single xml output file. The following code shows a neat-o way to deal with the name space. I also assume that you are putting one access record into one xml file, and then writing it out. If you have to append “many” records to one xml file, then my use of the xPath “//” will not work, and you have to create a new node and use “/” for your xPath referencing.

However there are MANY msxml examples, it usually just getting started. And as noted, unfortunately the vast majority of those examples leave out the HORRIFYING and MASSIVE and NASTY and PAINFUL issue of namespaces. However, the code will look something like this:

Sub xmlExport()

  Dim xml        As New MSXML2.DOMDocument60
  Dim xmldoc     As MSXML2.IXMLDOMNode

  Dim strTemplate   As String
  Dim strOutFile    As String

  strTemplate = "c:\test2\template.xml"

  If xml.Load(strTemplate) = False Then
     MsgBox "cannot open xml template"
  End If

  xml.SetProperty "SelectionLanguage", "XPath"
  xml.SetProperty "SelectionNamespaces", "xmlns:x='" & xml.namespaces(1) & "'"

  Dim rst        As DAO.Recordset
  Set rst = CurrentDb.OpenRecordset("SELECT * from tblDelivery where City = 'Edmonton'")

  Dim i As Integer

  Do While rst.EOF = False

     i = i + 1      ' sequence number
     xml.SelectSingleNode("//x:SequenceNo").Text = i
     xml.SelectSingleNode("//x:RsNumber").Text = rst!ResNumber
     xml.SelectSingleNode("//x:LicensePlate").Text = rst!License
     xml.SelectSingleNode("//x:FirstName").Text = rst!FirstName
     ' . etc . etc.

     strOutFile = "c:\outdata\xml" & i & ".xml"

     xml.Save (strOutFile)

     rst.MoveNext

  Loop

  rst.close
End Sub

Now having said above if anyone has a nice solution for working with xPath and a namespace, then I will say "thank you very much", and re-edit this post to remove the rant about world poverty and the pain and suffering xPath becomes when name spaces are involved.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice assessment of XML namespaces. They have cost me five years of my life.
Thank you for your solution I went for another option but i'm sure many people will find this information useful ! thanks again!
1

This problem has a very staightforward solution that does not involve getting involved with xml at all. You basically have a repeating data structure which is the text marked by

<REGDriverInformationData xsi:schemaLocation="http://www.Provider.fr/REGDriverInformation     REGDriverInformation.xsd " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.Provider.fr/REGDriverInformation">

data


</REGDriverInformationData>

Within that 'data' part you have elements

"<element>value</element>" 

which contain data you need to provide.

Create a dummy data part where the example data have been replace by a consistent dummy text so you have

"<element>****</element>"

You then populate each block of driver data using with a series of string replaces

e.g.

driver_data = replace(driver_data,"<License>****", "<License>" & DriverDataTable!License)
driver_data= replace(driver_data, "<Driver_FirstName>****", "Driver_FirstName" & DriverDataTable!FirstName)

Then when each driver datablock is complete just write it out to a standard text file.

1 Comment

Many thanks, this worked like a charm! 1500 records transformed into XML and send with REST API in 2,4 seconds with positive response code!
1

There are several approaches you could take, which is best is up for debate. That said I would choose to use the MSXML object library if VBA is a requirement, if it is not I would choose C# create the necessary classes and serialize.

1) Use the MSXML library, here is a good example of how to create a XML document using it. Creating XML with MSXML

2) Create classes and serialize using an external vba library available for download Serialize with vba library

3) Create using plain text - this could be messy and hard to maintain.

4) If you have a choice other than VBA, I would look at C# or VB.NET and create the required classes (or easily generate them from the XML) and then serialize.

2 Comments

Dear Kevin, I went for the messy way :) clearly I want to thank you for sharing this information, i'm sure it will be useful for others as well.
@Gregory - you are welcome, sometimes the messy way is the best way ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.