Solved Set different name to XML tag when exporting to XML file (1 Viewer)

Tea

Member
Local time
Today, 10:42
Joined
Oct 30, 2020
Messages
43
Hello, is it possible to change the name of my table tag when I'm exporting an XML file through VBA?

Exported file looks like this

XML:
-<tbl_xm_ct>
    <ID>44</ID>
    <desc>pol 3</desc>
    <phone>111155</phone>
 </tbl_xm_ct>

I want this

XML:
-<xml_contacts>
    <ID>44</ID>
    <desc>pol 3</desc>
    <phone>111155</phone>
</xml_contacts>

Is it possible to change it in the code somehow? I can't change the name of the table because of the convention.

Or generally how to set the name of the tags not from the table but custom.

Like this

XML:
-<xml_contacts>
    <ID>44</ID>
    <description>pol 3</desc>
    <phoneNumber>111155</phone>
</xml_contacts>

My VBA code looks like this

Code:
 Dim contacts As AdditionalData
Dim filter As String

Set contacts = Application.CreateAdditionalData

contacts .Add "tbl_xm_ct"

filter = Me.ID

Application.ExportXML ObjectType:=acExportTable, DataSource:="tbl_xm", _
DataTarget:="C:\Users\...\test.xml", _
WhereCondition:="ID=" & filter, _
AdditionalData:=contacts

Table tbl_xml is the first tag part and the table tbl_xml_ct is a nested table.

Thank you
 

bastanu

AWF VIP
Local time
Today, 01:42
Joined
Apr 13, 2010
Messages
1,174
Have a look at the attached sample I did for another thread on this forum with a similar issue. The idea is to export it like you do now then edit the output (the XML file itself) using Replace() to replace <tbl_xm_ct> with <xml_contacts> or whatever else you need. You can build that list in the table and loop through it if multiple replaces are needed.

Cheers,
 

Attachments

  • CSVExportSample.accdb
    660 KB · Views: 224

Tea

Member
Local time
Today, 10:42
Joined
Oct 30, 2020
Messages
43
Have a look at the attached sample I did for another thread on this forum with a similar issue. The idea is to export it like you do now then edit the output (the XML file itself) using Replace() to replace <tbl_xm_ct> with <xml_contacts> or whatever else you need. You can build that list in the table and loop through it if multiple replaces are needed.

Cheers,
What if the target location is not the same and every user can save it anywhere to their location directionary.
 

sonic8

AWF VIP
Local time
Today, 10:42
Joined
Oct 27, 2015
Messages
593
Is it possible to change it in the code somehow? I can't change the name of the table because of the convention.

Or generally how to set the name of the tags not from the table but custom.
Just an idea:
  1. Create a temporary query containing the data you want to export.
  2. Name that query the way you want the name to be in the exported XML. (Can also be applied to column names if required.)
  3. Export the XML
  4. Delete the query.
 
  • Like
Reactions: Tea

bastanu

AWF VIP
Local time
Today, 01:42
Joined
Apr 13, 2010
Messages
1,174
Probably sonic8's suggestion will be easier for you, but I don't understand your point in post # 3. I every instance you will now exactly where each user just saved the file because you control the path (see your other post with using FileDialog), so just after the export happens you invoke the replace.

Cheers,
 

Users who are viewing this thread

Top Bottom