- '示例:新建节点,新建xml文件
- Option Explicit
- Dim oDOMDocument, oXMLDOMElement, oXMLDOMNode, oNode
-
- Set oDOMDocument = CreateObject("Msxml2.DOMDocument")
-
- Set oNode = oDOMDocument.createProcessingInstruction("xml", "version='1.0' encoding='GB2312'")
- oDOMDocument.appendChild oNode 'xml声明节点,必须在第一行
-
- Set oNode = oDOMDocument.createProcessingInstruction("xml-css", "type=""text/css"" href=""t.css""")
- oDOMDocument.appendChild oNode '其它声明节点,可选
-
- Set oNode = oDOMDocument.createComment("注释行") '注释节点,可选
- oDOMDocument.appendChild oNode
-
- Set oXMLDOMElement = oDOMDocument.createElement("note") '根节点,必需
- oDOMDocument.appendChild oXMLDOMElement
-
- Set oNode = oDOMDocument.createNode(1, "n1", "") '元素节点n1,NODE_ELEMENT = 1
- oNode.text = "<>&'""" 'n1节点的文本。注意转义字符
- Set oXMLDOMNode = oDOMDocument.createAttribute("a1") 'n1节点的属性
- oXMLDOMNode.value = "20147578"
- oNode.setAttributeNode oXMLDOMNode
- Set oXMLDOMNode = oDOMDocument.createNode(1, "c1", "") 'n1节点的子节点c1
- oNode.appendChild oXMLDOMNode
- Set oXMLDOMNode = oDOMDocument.createNode(1, "c1", "") '同名子节点。必须重新创建
- oNode.appendChild oXMLDOMNode
- oXMLDOMElement.appendChild oNode '根节点的子节点n1
-
- Set oNode = oDOMDocument.createNode(1, "n2", "") '根节点的子节点n2
- oXMLDOMElement.appendChild oNode
- Set oNode = oDOMDocument.createNode(1, "n3", "") '根节点的子节点n3
- oXMLDOMElement.appendChild oNode
-
- Set oNode = oDOMDocument.createTextNode("QQ20147578") '文本节点
- oXMLDOMElement.appendChild oNode
-
- Set oNode = oDOMDocument.createEntityReference("lt") '实体引用节点,就是字符串<
- oXMLDOMElement.appendChild oNode
-
- Set oNode = oDOMDocument.createCDATASection("<>&'""") '非转义文本节点
- oXMLDOMElement.appendChild oNode
-
- MsgBox oDOMDocument.xml '这里没有encoding,但写入文件后有encoding
-
- oDOMDocument.save "D:\1.xml" '覆盖写入文件
复制代码
|