|
Question : How do I modify an XML file from Access VBA
|
|
I have file with a "qsp" extension (I changed it to txt so I could attach it) that is used by another application and the file appears to be an XML file (testjorge.txt attached). I need to replace specific text "ListValues=One\,Two\,Three\,Four\,five\,six\,seven" from the file with values from a table. Example the "ListValues=" statement would list the values from the table instead of the "One\,Two\,Three\,Four\,five\,six\,seven". The file will be updated regularly so it needs to be handled maybe in VBA? Any help would be greatly appriciated.
|
|
Answer : How do I modify an XML file from Access VBA
|
|
i'd do it by using VBA to open a new file for input, and read in the lines from your file, testing for dataline = "ListValues=One\,Two\,Three\,Four\,five\,six\,seven,", then manufacture and output the new line, then continue reading the lines from the course document, to make a new file...
a loop to pick up the values from the table and concetenate them into a string would not be too tricky.
this could all be done using fancy recordsets, or more lazily, from a form with a combo box controlling the data.
The attached MDB has a form which takes the path to the xml file, has a combo picking up the records id, xvalue, where xvalue column is the actual new values to insert, from table1.
the test output is created as filename_out.xml, but i had to rename it txt to upload it here.
the code from the form is below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
|
Option Compare Database
Dim strFilepath As String
Dim strFilepathOut As String
Dim DataLine As String
Dim OutLine As String
Dim FindValues As String
Dim ReplacementValues As String
Dim Counter As Integer
Private Sub CmdProcess_Click()
DoCmd.Hourglass True
OldOpen
DoCmd.Hourglass False
End Sub
Public Sub OldOpen()
'initialise
strFilepath = Replace(Me.txtfile, ".xml", ".txt", 1, -1)
strFilepathOut = Replace(Me.txtfile, ".xml", "_Out.xml", 1, -1)
Counter = 0
LINECounter = 0
OutLine = ""
FindValues = "ListValues=One\,Two\,Three\,Four\,five\,six\,seven"
'should the above have a \ after seven?
'"ListValues=One\,Two\,Three\,Four\,five\,six\,seven\"
ReplacementValues = ""
'calculate replace value...
'i have a combo box selecting the records from my table, table1....
For Counter = 0 To Me.Combo5.ListCount - 1
If Counter = 0 Then
ReplacementValues = ReplacementValues & Me.Combo5.Column(1, Counter)
Else
ReplacementValues = ReplacementValues & "\," & Me.Combo5.Column(1, Counter)
End If
Next Counter
ReplacementValues = ReplacementValues & ","
MsgBox ReplacementValues
'getXML file renamed as txt
FileCopy Me.txtfile, strFilepath
Open strFilepath For Input As #1
Open strFilepathOut For Output As #2
' Read the data one line at a time.
While Not EOF(1)
Line Input #1, DataLine
If InStr(DataLine, FindValues) > 0 Then
' if the current line is the one to change
OutLine = Replace(DataLine, FindValues, ReplacementValues, 1, 1)
Else
OutLine = DataLine
End If
'output the (new) line to the new file
Print #2, OutLine
Wend
Close #1
Close #2
'tidy up
'FileCopy strFilepathOut, Me.txtfile
Kill strFilepath
End Sub
|
|
|
|
|