SetNamedItem (MSXML)
Aus API-Wiki
Zur Navigation springenZur Suche springenMit setNamedItem kann einem XML-Knoten ein Attribut hinzugefügt werden.
Set Result = object.attributes.setNamedItem(newAttribute)
Parameter
object
- (erforderlich) Dokument oder Knoten, dem ein neues Attribut hinzugefügt werden soll (Datentyp DOMDocument, FreeThreadedDOMDocument oder IXMLDOMNode).
newAttribute
- (erforderlich) Attribut, welches hinzugefügt werden soll (Datentyp IXMLDOMAttribute)
Rückgabe
Result
- (optional) Verweis auf das eingefügte Attribut (Datentyp IXMLDOMAttribute).
Hinweise
Sollte der Knoten das hinzuzufügende Attribut bereits besitzen, so wird der Wert des vorhandenen Attributs mit dem neuen Wert überschrieben.
Beispiel
Dim objXml As MSXML2.FreeThreadedDOMDocument
Dim strXml As String
Dim objNode As MSXML2.IXMLDOMNode
Dim objAttr As MSXML2.IXMLDOMAttribute
strXml = VBA.Constants.vbNullString
strXml = strXml & "<?xml version=""1.0""?>" & VBA.Constants.vbNewLine
strXml = strXml & "<bibliothek>" & VBA.Constants.vbNewLine
strXml = strXml & vbTab & "<buecher>" & VBA.Constants.vbNewLine
strXml = strXml & vbTab & vbTab & "<buch name=""VB für Dummies"" />" & VBA.Constants.vbNewLine
strXml = strXml & vbTab & vbTab & "<buch name=""Turbo Pascal für Dummies"" />" & VBA.Constants.vbNewLine
strXml = strXml & vbTab & "</buecher>" & VBA.Constants.vbNewLine
strXml = strXml & "</bibliothek>" & VBA.Constants.vbNewLine
Set objXml = New MSXML2.FreeThreadedDOMDocument
With objXml
.async = False
.validateOnParse = True
If .loadXML(strXml) Then
' Knoten auswählen, der ein neues Attribut erhalten soll
Set objNode = .selectSingleNode("/bibliothek/buecher/buch[@name='VB für Dummies']")
' Neues Attribut erzeugen
Set objAttr = .createAttribute("bewertung")
objAttr.nodeValue = "sehr gut"
' Attribut einfügen
Call objNode.Attributes.setNamedItem(objAttr)
' Geändertes XML-Dokument anzeigen
Debug.Print .xml
Else
Debug.Print "[Fehler] " & .parseError.reason
End If
End With
Set objAttr = Nothing
Set objNode = Nothing
Set objXml = Nothing