我正在excel 2013中创建一个xml文档,所以我使用vba复制另一个xml文档。下面我编写了一些代码来创建和保存我的xml文档。文档中的数据是良好的,结构正确。然而,有两样东西不见了。我在我的项目中引用了MicrosoftXMLVersion3.0。
第一行缺失的是xml文档,但在我试图复制的一行中,
<?xml version="1.0" encoding="utf-8" ?>第二行也不同。我有过,
-<MyXmlReport>我要复制的文件有,
-<MyXmlReport xmlns:xsi="http://www.w3.org/2001/XMLSchem-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema下面是我的代码(目前只用于测试)。我想,当您创建一个DOMDocument时,上面的第一行将被添加到文档中?
更新
我在objDom.setProperty下面添加了一行,当我使用getProperty("SelectionNamespaces")方法时,名称空间似乎在那里,但是当我打开文档时,仍然看不到名称空间?
Public Sub CreateXMLDoc()
Dim objDom As DOMDocument
Dim objRootElem As IXMLDOMElement
Dim objMemElem As IXMLDOMElement
Set objDom = New DOMDocument
objDom.setProperty "SelectionNamespaces", "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""" & " xmlns:xsd=""http://www.w3.org/2001/XMLSchema"""
' create the root element
Set objRootElem = objDom.createElement("MyXmlReport")
objDom.appendChild objRootElem
' create security element
Dim objSecElem As IXMLDOMElement
Set objSecElem = objDom.createElement("Security")
objRootElem.appendChild objSecElem
Dim str(1 To 3) As String
str(1) = "A"
str(2) = "B"
str(3) = "C"
For i = 1 To UBound(str)
Dim objProp As IXMLDOMElement
Set objProp = objDom.createElement(str(i))
objSecElem.appendChild objProp
objProp.Text = i
Next i
objDom.Save "C:\Somelocation\myFile.xml"
end sub发布于 2014-08-06 19:14:52
您需要将XML声明添加为处理指令(David已经讨论过这一点):
Dim xmlDecl As IXMLDOMProcessingInstruction
Set xmlDecl = objDom.createProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8""")
objDom.appendChild xmlDecl命名空间声明是根元素上的属性,因此可以使用以下方法添加它们:
objRootElem.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
objRootElem.setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"发布于 2014-08-06 19:00:39
您需要创建处理指令或XML声明。尝尝这个
Dim declaration
Set declaration = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8' standalone='no' ")
objDom.appendChild declarationhttps://stackoverflow.com/questions/25164838
复制相似问题