我正在尝试在旧的 Access 应用程序中创建 SEPA XML 文件。
我发现第一行有一个错误。
Dim xmlDom As MSXML2.DOMDocument60
Dim xmlspacename As MSXML2.IXMLDOMElement
Dim xmlVersion As MSXML2.IXMLDOMProcessingInstruction
Dim xslStylesheet As MSXML2.IXMLDOMProcessingInstruction
Set xmlDom = New MSXML2.DOMDocument60
Set xmlVersion = xmlDom.createProcessingInstruction("xml", "version='1.0' encoding= 'UTF-8'")
xmlDom.appendChild xmlVersion
Set xmlspacename = xmlDom.createElement("Document")
xmlDom.appendChild xmlspacename
xmlDom.documentElement.setAttribute "xmlns", "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
xmlDom.documentElement.setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"
xmlDom.documentElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
'****RAIZ DEL MENSAJE
'Creando elemento raiz [1..1]_0
Dim xmlrootnode As IXMLDOMElement
Set xmlrootnode = xmlDom.createElement("CstmrCdtTrfInitn")
xmlspacename.appendChild xmlrootnode
'*****CABECERA
' Creando 1_0_cabecera [1..1]_0
Dim cabecera As IXMLDOMElement
Set cabecera = xmlDom.createElement("GrpHdr")
xmlrootnode.appendChild cabecera
MsgBox "Ok"
' Saves XML data to disk.
xmlDom.Save ("c:\temp\andrew.xml")
输出:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn xmlns="">
<GrpHdr/>
</CstmrCdtTrfInitn>
</Document>
这是生成的 xml 文件。我将它与银行给我的一些示例文件进行了比较,这个标签有一个区别:
<CstmrCdtTrfInitn xmlns="">
我需要属性特殊词“xmlns”从标签中消失。
尝试此解决方法:
Dim xmlDom As MSXML2.DOMDocument60
Dim xmlspacename As MSXML2.IXMLDOMElement
Dim xmlVersion As MSXML2.IXMLDOMProcessingInstruction
Dim xslStylesheet As MSXML2.IXMLDOMProcessingInstruction
Set xmlDom = New MSXML2.DOMDocument60
'Set xmlVersion = xmlDom.createProcessingInstruction("xml", "version='1.0' encoding= 'UTF-8'")
'xmlDom.appendChild xmlVersion
Set xmlspacename = xmlDom.createElement("Document")
xmlDom.appendChild xmlspacename
'xmlDom.DocumentElement.setAttribute "xmlns", "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
'xmlDom.DocumentElement.setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"
'xmlDom.DocumentElement.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
'****RAIZ DEL MENSAJE
'Creando elemento raiz [1..1]_0
Dim xmlrootnode As IXMLDOMElement
Set xmlrootnode = xmlDom.createElement("CstmrCdtTrfInitn")
xmlspacename.appendChild xmlrootnode
'*****CABECERA
' Creando 1_0_cabecera [1..1]_0
Dim cabecera As IXMLDOMElement
Set cabecera = xmlDom.createElement("GrpHdr")
xmlrootnode.appendChild cabecera
'https://stackoverflow.com/questions/2596085/saving-xml-in-utf-8-with-msxml
Dim objStream As Stream, strData As String, sFilePath As String
'MsgBox (xmlDom.XML)
'Debug.Print xmlDom.XML
strData = Replace(xmlDom.XML, "<Document>", "<Document xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"">")
sFilePath = ThisWorkbook.Path & "\andrew.xml"
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeText
objStream.charset = "utf-8"
objStream.LineSeparator = adCRLF
'objStream.LineSeparator = adLF
objStream.Open
objStream.WriteText "<?xml version='1.0' encoding='utf-8'>", adWriteLine
objStream.WriteText strData, adWriteChar
objStream.SaveToFile sFilePath, adSaveCreateOverWrite