I've found some items that seem to dance around what I'm looking to do. I'm not a full-fledged programmer, but have been creating my own macros in Excel. I'm trying to create one now that will help me create the structure of an XML file.
For this context I have a column of Xpaths and one of associated values. I want to go down the list of XPaths, and for each one create any parents in the path as needed as well as the child and its value. I'll be adding another aspect to filter it to the fields I want, but the part I'm struggling with is the logic to check if each parent node exists, create it if needed, then add the child and its value.
Bonus would be if the output file were formatted with each node on a new line, and more so if it child nodes were indented from parent, but just creating it is my primary concern. Any help would be much appreciated, this has to potential to save me a lot of time.
Update: I've made progress on this, and was able to create a small sample file. I'm still not sure if there's a way to format the file with line returns and/or tab indents, but I think the main part of creating the file is well underway. I still need to look at how to handle iterative nodes. Here is what I have so far, if anyone has any critiques, it's welcome:
Sub main()
Dim xmldoc As New DOMDocument60
Dim xpath As String
Dim outPath As String
outPath = "c:\temp\test.xml"
'just a small sample for now, xpaths in column 2 on spreadsheet
For i = 2 To 5
'xpaths I have leave off root element, so I add it here
xpath = "session/" & Cells(i, 2).Value
Call makeXPath(xmldoc, xpath)
Next
xmldoc.Save outPath
End Sub
Private Sub makeXPath(xmldoc As DOMDocument60, xpath As String)
Dim partsOfPath() As String
Dim oNodeList As IXMLDOMNodeList
Dim strXPathQuery As String
Dim sParent As String
Dim objRootElem As IXMLDOMElement
Dim objMemberElem As IXMLDOMElement
Dim objMemberName As IXMLDOMElement
Dim objParent As Object
Set objParent = xmldoc
partsOfPath = Split(xpath, "/")
For i = LBound(partsOfPath) To UBound(partsOfPath)
If strXPathQuery > "" Then strXPathQuery = strXPathQuery & "/"
strXPathQuery = strXPathQuery & partsOfPath(i)
Set oNodeList = xmldoc.SelectNodes(strXPathQuery)
If oNodeList.Length = 0 Then
'if I don't have the node, create it
Set objMemberElem = xmldoc.createElement(partsOfPath(i))
objParent.appendChild objMemberElem
'setting the parent for the next element of the path
Set objParent = objMemberElem
Else
'setting parent to first iteration, until I make adjustment otherwise later
Set objParent = oNodeList.Item(0)
End If
Next
End Sub
Update2 - Here is a sample of xpaths and example output that has a 2nd iteration of address:
data/account/AcctNumber
data/account/Phone
data/account/address/Address1
data/account/address/City
data/account/address/State
data/account/address/ZIP
data/account/address/AddressType
<data>
<account>
<AcctNumber>
<Phone>
<address>
<Address1 />
<City />
<State />
<ZIP />
<AddressType />
</address>
<address>
<Address1 />
<City />
<State />
<ZIP />
<AddressType />
</address>
</account>
</data>