Excel table to XML Vba converter is putting all my data rows into one tag as attributes? -
i asked question yesterday , got no answers. trying transform , excel table xml. below have (number of children reduced overall format repeated). issue have when increase number of rows, gives me this.
<root> <random> <random1 a="a2" b="b2" <random1 a='a3' b="b3" > <cookie> <orange c="c2" d="d2" <orange c="c3" d="d3"/> </cookie> </random1> <random> <root> i format.
<root> <random> <random1 a="a2" b="b2"> <cookie> <orange c="c2" d="d2"/> </cookie> </random1> <random1 a='a3' b="b3" > <cookie> <orange c="c3" d="d3"/> </cookie> </random1> <random> <root> basically, how data appear consecutively instead of in 1 line? code below.
sub toxml() myfile = "data.xml" fnum = freefile() dim n integer n = 2 'number of data rows dim m integer m = 104 ' number of columns print #fnum, "<root>" print #fnum, " < random >" = 2 n + 1 print #fnum, " <random1 "; j = 1 39 print #fnum, cells(1, j).value & "=""" & cells(i, j).value & """ "; next j next print #fnum, ">" print #fnum, " < cookie >" i2 = 2 n + 1 print #fnum, " <orange "; j2 = 40 42 print #fnum, cells(1, j2).value & "=""" & cells(i, j2).value & """ "; next j2 next i2 print #fnum, "/>" print #fnum, " <cookie>" print #fnum, " random1>" print #fnum, " </random>" print #fnum, "</ root >" 'close root node
you should work arrays , build each attribute before writing row:
dim headers(), data(), attributes1(), attributes2(), attr$, r&, c& ' load headers , data array ' headers = cells(1, 1).resize(1, 42).value data = cells(1, 2).resize(2, 42).value ' set size attributes ' redim attributes1(1 39) redim attributes2(40 42) ' open file , print header ' open "c:\temp\output.xml" output #1 print #1, "<root>" print #1, " <random>" ' iterate each row ' r = 1 ubound(data) ' iterate each column ' c = 1 ubound(data, 2) ' build each attribute ' attr = headers(1, c) & "=""" & data(r, c) & """" if c <= 39 attributes1(c) = attr else attributes2(c) = attr end if next ' print row ' print #1, " <random1 " & join(attributes1, " ") & " >" print #1, " <cookie>" print #1, " <orange " & join(attributes2, " ") & " />" print #1, " </cookie>" print #1, " </random1>" next ' print footer , close ' print #1, " </random>" print #1, "</root>" close #1
Comments
Post a Comment