我正在为我的办公室工作构建一个xml文件。 我要求xml的结构如下
<MultiBlock xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="x-
schema:ConfigFileSchema.xml">
<Model>
<ModelName type="AlarmGroup">Alarm Groups</ModelName>
</Model>
<ErdbVersion>
<DbVersion>12.0</DbVersion>
</ErdbVersion>
</MultiBlock>
和我的代码是:
Sub create_alarmgroup()
Dim objDom As DOMDocument
Dim objRootElem As IXMLDOMElement
Dim objMemberElem As IXMLDOMElement
Dim objmembervar As IXMLDOMElement
Dim objmemberatt As IXMLDOMAttribute
Const QUOTE_MARK As Integer = 34
Dim xmlVersion As MSXML2.IXMLDOMProcessingInstruction
Set objDom = New DOMDocument
Set xmlVersion = objDom.createProcessingInstruction("xml", "version=" &
Chr(QUOTE_MARK) & "1.0" & Chr(QUOTE_MARK))
objDom.appendChild xmlVersion
'Creates root element
Set objRootElem = objDom.createElement("MultiBlock")
objRootElem.setAttribute "xmlns", "x-schema:ConfigFileSchema.xml"
objRootElem.setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"
objRootElem.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-
instance"
objDom.appendChild objRootElem
'==========ROOT ELEMENT END================
'Create Member element - MODEL
Set objMemberElem = objDom.createElement("Model")
objMemberElem.Text = " "
objRootElem.appendChild objMemberElem
Set objmembervar = objDom.createElement("ModelName")
Set objmemberatt = objDom.createAttribute("type")
objmemberatt.NodeValue = "AlarmGroup"
objmembervar.setAttributeNode objmemberatt
objmembervar.Text = "Alarm Groups"
objMemberElem.appendChild objmembervar
Set objMemberElem = objDom.createElement("ErdbVersion")
objMemberElem.Text = " "
objRootElem.appendChild objMemberElem
Set objmembervar = objDom.createElement("DbVersion")
objmembervar.Text = "12.0"
objRootElem.appendChild objmembervar
'Save to disk
Cells(5, 1).Select
objDom.Save (ThisWorkbook.Path & "\srcvariables.cnf.xml")
End Sub
但我无法在&#34; Model&#34;,&#34; ErdbVersion&#34;中创建结束标记。节点。我不想为我的xml使用自闭标签,需要传统的xml。
请指导。
答案 0 :(得分:0)
我找到了添加结束标记的方法。 我已将xml元素的文本属性设置为&#34; &#34;
xmlelement.text= " "