Создать родительский узел в существующих XML узлах, используя VBA - PullRequest
0 голосов
/ 30 апреля 2020

извините, я хотел бы переписать его, чтобы сделать его простым:

My XML Структура:

<component type = "A">
<component type = "B">

//other nodes and elements are here

</component>
</component>

Ожидание: поместить другой компонент с типом "C" между типами = A и B и сделать тип B дочерним узлом нового типа узла "C".

<component type = "A">
<component type ="C"> //This is the new element
<component type = "B">
    //other nodes here

</component>
</component> //this is the new closing tag
</component>

Код: Попытка добавить новый элемент перед типом "B", но это результат:

<component type = "A">
<component type ="C"> //this the new element
</component> //this is the new closing tag
<component type = "B">

    //other nodes here

</component>
</component>

Dim fileName As String
fileName = ActiveSheet.OLEObjects("TextBox1").Object.Text
XMLFileName = fileName
Dim Found As Boolean
Dim docXMLDOM As DOMDocument
Dim nodRoot As IXMLDOMElement
Dim nodElement As IXMLDOMElement
Dim elements_Component As IXMLDOMNodeList
Dim nodNewElement As IXMLDOMElement
Dim nodReference As IXMLDOMElement  
Set docXMLDOM = New DOMDocument
docXMLDOM.Load XMLFileName
Set elements_Component = docXMLDOM.getElementsByTagName("component")

For Each nodElement In elements_Component
If InStr(nodElement.Attributes.getNamedItem("type").Text, "B") Then
Set nodReference = nodElement
Found = True
Exit For
End If
Next

If Found = True Then
Set nodRoot = docXMLDOM.documentElement
Set nodNewElement = docXMLDOM.createElement("component")
nodNewElement.setAttribute "type", "C"
nodRoot.insertBefore nodNewElement, nodReference.parentNode
docXMLDOM.Save XMLFileName
End If 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...