XML出力 with MS XML 6.0 (1)

Sub writeXML()

Dim xmlDoc As MSXML2.DOMDocument

Dim xmlPI As MSXML2.IXMLDOMProcessingInstruction

Dim xmlNode As MSXML2.IXMLDOMNode

Dim xmlAttr As MSXML2.IXMLDOMAttribute

Dim xmlString As String

Set xmlDoc = New MSXML2.DOMDocument

Set xmlPI = xmlDoc.appendChild(xmlDoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8"""))

Set xmlNode = xmlDoc.appendChild(xmlDoc.createElement("root"))

Set xmlNode = xmlNode.appendChild(xmlDoc.createElement("child"))

Set xmlAttr = xmlNode.Attributes.setNamedItem(xmlDoc.createAttribute("id"))

xmlAttr.NodeValue = "001"

Set xmlNode = xmlNode.appendChild(xmlDoc.createTextNode("日本語"))

xmlString = xmlDoc.XML

Debug.Print xmlString

xmlDoc.Save ("C:\\test.xml")

End Sub