用vba编写复杂的命名空间xml

时间:2017-05-16 21:46:33

标签: xml excel-vba vba excel

所以...另一个xml命名空间问题。我想用vba

编写这个命名空间
Option Explicit

Public Sub Create_Database()

'Declare document objects
Dim xDoc As MSXML2.DOMDocument60
Dim xNode As IXMLDOMNode
Dim xRoot As IXMLDOMElement

'create new DOMDocument
Set xDoc = New DOMDocument60

'Create processing instructions
Set xNode = xDoc.createProcessingInstruction("xml", "version='1.0' encoding= 'UTF-8'")
Set xNode = xDoc.InsertBefore(xNode, xDoc.ChildNodes.Item(0))

'create root element
Set xRoot = xDoc.createElement("Datas")
xDoc.appendChild xRoot
xDoc.DocumentElement.setAttribute "xsi:schemaLocation", "uri:rhubarb:pie RHUBARB%20PIE%202012.xsd"
xDoc.DocumentElement.setAttribute "xmlns", "uri:rhubarb:pie"
xDoc.DocumentElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"

'save xml file
xDoc.Save "C:\Users\danny\Desktop\xml\Database.xml"
'clear xDoc from memory
Set xDoc = Nothing
End Sub

这是我到目前为止的代码

xDoc.DocumentElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"

...但我知道在命名空间中我有太多的参数。我在网上找到的唯一一件事就是像这样编写我的命名空间:

{{1}}

我不断收到太多参数的错误。我的问题:如何编写更复杂的命名空间?或者这是一种正确的写作方式吗?

1 个答案:

答案 0 :(得分:2)

不用担心我已经弄清楚了。如果其他人正在寻找相同的东西,我会编辑它!

我改变了这个:

xDoc.DocumentElement.setAttribute "xsi:schemaLocation", "uri:rhubarb:pie RHUBARB%20PIE%202012.xsd", "xmlns", "uri:rhubarb:pie", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"

对此:

xDoc.DocumentElement.setAttribute "xsi:schemaLocation", "uri:rhubarb:pie RHUBARB%20PIE%202012.xsd"
xDoc.DocumentElement.setAttribute "xmlns", "uri:rhubarb:pie"
xDoc.DocumentElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"