new user and infrequent/inexperienced coder here. I found a solution awhile back on this site for a VBA macro that creates an XML file for each line of a spreadsheet. I work at an archives and our digital repository system requires XML metadata files that have the same filename (with an added .metadata extension) as the file they describe; this is so the system will recognize it as metadata and not a discrete file. To achieve this, we record metadata in a spreadsheet with column headings that match our metadata schema elements and run a VBA macro to create an XML file for each row of data.
The macro actually works perfectly for creating individual XML files from each row of a spreadsheet. The problem occurred after we updated our metadata schema to support repeated elements. When I run the VBA macro on a spreadsheet with repeated column headings/elements the resulting XML file only has data from the last instance of the repeated element. This same data value from the last repeated element is also applied to the previous instances.
Here's what I'm talking about. As you can see, the repeated "RecordContributorIndividual" elements in the XML file have only the data from the final instance of the element (Row 1, Column 7) in the spreadsheet:
<?xml version="1.0" encoding="UTF-8"?>
<vtcore xmlns="http://www.sec.state.vt.us/vtcore">
<RecordCreatorIndividual>Peter Shumlin</RecordCreatorIndividual>
<RecordContributorIndividual>Stuck</RecordContributorIndividual>
<RecordContributorIndividual>Stuck</RecordContributorIndividual>
<RecordContributorIndividual>Stuck</RecordContributorIndividual>
<RecordContributorIndividual>Stuck</RecordContributorIndividual>
<RecordContributorIndividual>Stuck</RecordContributorIndividual>
<RecordTitle>President Ronald Reagan Day proclamation</RecordTitle>
<RecordDesc></RecordDesc>
What I want to achieve is a VBA code that will not apply the last cell value of a repeated element to all of the previous instances of that element but will instead pull whatever is actually in the spreadsheet cell under each element. I've pasted the VBA code below. I have a feeling that the problem lies somewhere down in the "doc.getElementsByTagName" region but I'm not positive. I feel like I'm close but I'm completely stuck. Any help is greatly appreciated!
Sub testXLSMtovtcoreXML()
sTemplateXML = _
"<?xml version='1.0' encoding='UTF-8'?>" + vbNewLine + _
"<vtcore xmlns='http://www.sec.state.vt.us/vtcore'>" + vbNewLine + _
" <RecordCreatorIndividual>" + " </RecordCreatorIndividual>" + "
<RecordContributorIndividual>" + " </RecordContributorIndividual>" +
vbNewLine + _
" <RecordContributorIndividual>" + " </RecordContributorIndividual>"
+ " <RecordContributorIndividual>" + "
</RecordContributorIndividual>" + vbNewLine + _
" <RecordContributorIndividual>" + " </RecordContributorIndividual>"
+ " <RecordContributorIndividual>" + "
</RecordContributorIndividual>" + vbNewLine + _
" <RecordTitle>" + " </RecordTitle>" + " <RecordDesc>" + "
</RecordDesc>" + " <RecordDate>" + " </RecordDate>" + "
<RecordDate>" + " </RecordDate>" + vbNewLine + _
" <RecordDate>" + " </RecordDate>" + " <RecordDate>" + "
</RecordDate>" + " <RecordDate>" + " </RecordDate>" + vbNewLine + _
" <Agency>" + " </Agency>" + " <Domain>" + " </Domain>" + "
<Activity>" + " </Activity>" + " <RecordType>" + " </RecordType>"
+ vbNewLine + _
" <ClassificationCode>" + " </ClassificationCode>" + "
<RelatedRecords>" + " </RelatedRecords>" + " <RelatedRecords>" + "
</RelatedRecords>" + vbNewLine + _
" <RelatedRecords>" + " </RelatedRecords>" + " <RelatedRecords>" +
" </RelatedRecords>" + " <RelatedRecords>" + " </RelatedRecords>"
+ vbNewLine + _
" <RecordIdentifier>" + " </RecordIdentifier>" + " <PublicAccess>"
+ " </PublicAccess>" + " <PublicAccessCitation>" + "
</PublicAccessCitation>" + vbNewLine + _
" <PublicAccessCitation>" + " </PublicAccessCitation>" + "
<PublicAccessCitation>" + " </PublicAccessCitation>" + vbNewLine + _
" <PublicAccessCitation>" + " </PublicAccessCitation>" + "
<PublicAccessCitation>" + " </PublicAccessCitation>" + vbNewLine + _
" <Subject>" + " </Subject>" + " <Subject>" + " </Subject>" + "
<Subject>" + " </Subject>" + " <Subject>" + " </Subject>" +
vbNewLine + _
" <Subject>" + " </Subject>" + vbNewLine + _
"</vtcore>" + vbNewLine
Set doc = CreateObject("MSXML2.DOMDocument")
doc.async = False
doc.validateOnParse = False
doc.resolveExternals = False
With ActiveWorkbook.Worksheets(1)
lLastRow = .UsedRange.Rows.Count
For lRow = 2 To lLastRow
sFileName = .Cells(lRow, 1).Value
sRecordCreatorIndividual = .Cells(lRow, 2).Value
sRecordContributorIndividual = .Cells(lRow, 3).Value
sRecordContributorIndividual = .Cells(lRow, 4).Value
sRecordContributorIndividual = .Cells(lRow, 5).Value
sRecordContributorIndividual = .Cells(lRow, 6).Value
sRecordContributorIndividual = .Cells(lRow, 7).Value
sRecordTitle = .Cells(lRow, 8).Value
sRecordDesc = .Cells(lRow, 9).Value
sRecordDate = .Cells(lRow, 10).Value
sRecordDate = .Cells(lRow, 11).Value
sRecordDate = .Cells(lRow, 12).Value
sRecordDate = .Cells(lRow, 13).Value
sRecordDate = .Cells(lRow, 14).Value
sAgency = .Cells(lRow, 15).Value
sDomain = .Cells(lRow, 16).Value
sActivity = .Cells(lRow, 17).Value
sRecordType = .Cells(lRow, 18).Value
sClassificationCode = .Cells(lRow, 19).Value
sRelatedRecords = .Cells(lRow, 20).Value
sRelatedRecords = .Cells(lRow, 21).Value
sRelatedRecords = .Cells(lRow, 22).Value
sRelatedRecords = .Cells(lRow, 23).Value
sRelatedRecords = .Cells(lRow, 24).Value
sRecordIdentifier = .Cells(lRow, 25).Value
sPublicAccess = .Cells(lRow, 26).Value
sPublicAccessCitation = .Cells(lRow, 27).Value
sPublicAccessCitation = .Cells(lRow, 28).Value
sPublicAccessCitation = .Cells(lRow, 29).Value
sPublicAccessCitation = .Cells(lRow, 30).Value
sPublicAccessCitation = .Cells(lRow, 31).Value
sSubject = .Cells(lRow, 32).Value
sSubject = .Cells(lRow, 33).Value
sSubject = .Cells(lRow, 34).Value
sSubject = .Cells(lRow, 35).Value
sSubject = .Cells(lRow, 36).Value
doc.LoadXML sTemplateXML
doc.getElementsByTagName("RecordCreatorIndividual")(0).appendChild
doc.createTextNode(sRecordCreatorIndividual)
doc.getElementsByTagName("RecordContributorIndividual")(0).appendChild
doc.createTextNode(sRecordContributorIndividual)
doc.getElementsByTagName("RecordContributorIndividual")(1).appendChild
doc.createTextNode(sRecordContributorIndividual)
doc.getElementsByTagName("RecordContributorIndividual")(2).appendChild
doc.createTextNode(sRecordContributorIndividual)
doc.getElementsByTagName("RecordContributorIndividual")(3).appendChild
doc.createTextNode(sRecordContributorIndividual)
doc.getElementsByTagName("RecordContributorIndividual")(4).appendChild
doc.createTextNode(sRecordContributorIndividual)
doc.getElementsByTagName("RecordTitle")(0).appendChild
doc.createTextNode(sRecordTitle)
doc.getElementsByTagName("RecordDesc")(0).appendChild
doc.createTextNode(sRecordDesc)
doc.getElementsByTagName("RecordDate")(0).appendChild
doc.createTextNode(sRecordDate)
doc.getElementsByTagName("RecordDate")(1).appendChild
doc.createTextNode(sRecordDate)
doc.getElementsByTagName("RecordDate")(2).appendChild
doc.createTextNode(sRecordDate)
doc.getElementsByTagName("RecordDate")(3).appendChild
doc.createTextNode(sRecordDate)
doc.getElementsByTagName("RecordDate")(4).appendChild
doc.createTextNode(sRecordDate)
doc.getElementsByTagName("Agency")(0).appendChild
doc.createTextNode(sAgency)
doc.getElementsByTagName("Domain")(0).appendChild
doc.createTextNode(sDomain)
doc.getElementsByTagName("Activity")(0).appendChild
doc.createTextNode(sActivity)
doc.getElementsByTagName("RecordType")(0).appendChild
doc.createTextNode(sRecordType)
doc.getElementsByTagName("ClassificationCode")(0).appendChild
doc.createTextNode(sClassificationCode)
doc.getElementsByTagName("RelatedRecords")(0).appendChild
doc.createTextNode(sRelatedRecords)
doc.getElementsByTagName("RelatedRecords")(1).appendChild
doc.createTextNode(sRelatedRecords)
doc.getElementsByTagName("RelatedRecords")(2).appendChild
doc.createTextNode(sRelatedRecords)
doc.getElementsByTagName("RelatedRecords")(3).appendChild
doc.createTextNode(sRelatedRecords)
doc.getElementsByTagName("RelatedRecords")(4).appendChild
doc.createTextNode(sRelatedRecords)
doc.getElementsByTagName("RecordIdentifier")(0).appendChild
doc.createTextNode(sRecordIdentifier)
doc.getElementsByTagName("PublicAccess")(0).appendChild
doc.createTextNode(sPublicAccess)
doc.getElementsByTagName("PublicAccessCitation")(0).appendChild
doc.createTextNode(sPublicAccessCitation)
doc.getElementsByTagName("PublicAccessCitation")(1).appendChild
doc.createTextNode(sPublicAccessCitation)
doc.getElementsByTagName("PublicAccessCitation")(2).appendChild
doc.createTextNode(sPublicAccessCitation)
doc.getElementsByTagName("PublicAccessCitation")(3).appendChild
doc.createTextNode(sPublicAccessCitation)
doc.getElementsByTagName("PublicAccessCitation")(4).appendChild
doc.createTextNode(sPublicAccessCitation)
doc.getElementsByTagName("Subject")(0).appendChild
doc.createTextNode(sSubject)
doc.getElementsByTagName("Subject")(1).appendChild
doc.createTextNode(sSubject)
doc.getElementsByTagName("Subject")(2).appendChild
doc.createTextNode(sSubject)
doc.getElementsByTagName("Subject")(3).appendChild
doc.createTextNode(sSubject)
doc.getElementsByTagName("Subject")(4).appendChild
doc.createTextNode(sSubject)
doc.Save sFileName + ".metadata"
Next
End With
End Sub