I am using the below code to enable the user to select an XML file, and then the code deletes the <metadata>
tag from the XML, and replaces it by the modified one;
Sub Button1_Click()
Dim fso As Object, ts As Object, doc As Object
Dim data As Object, filename As String
Dim ws As Worksheet
Set ws = ActiveSheet
' select file
With Application.FileDialog(msoFileDialogFilePicker)
If .Show <> -1 Then Exit Sub
filename = .SelectedItems(1)
End With
' read file and add top level
Set doc = CreateObject("MSXML2.DOMDocument.6.0")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpentextFile(filename)
doc.LoadXML Replace(ts.readall, "<metadata>", "<root><metadata>", 1, 1) & "</root>" '<metadata> removed
ts.Close
' import data tag only
Dim s As String
Set data = doc.getElementsByTagName("data")(0)
s = data.XML
' MsgBox s
Set ts = fso.CreateTextFile(filename, True)
ts.Write s
ts.Close
MsgBox s 'works perfectly
End Sub
The above code worked perfectly for me, when I was assigned to work with an XML like this -
But now, I have a different XML to deal with, which is like - (difference : the ajson
root tag)
How do I delete the ajson
opening and closing tags, so I get my desired result? Kindly guide... Thanks!