作者:书友31617792 | 来源:互联网 | 2023-08-01 10:32
我是XML的新手。我使用以下函数创建一个xml文件。
我想更改代码以向其根元素添加更多详细信息。
您可以参考所附图片以达到我的目的。
Function fnCreateXmlNew()
Dim objDom As DOMDocument
Dim objRootElem As IXMLDOMElement
Dim objMemberElem As IXMLDOMElement
Dim objMemberRel As IXMLDOMAttribute
Dim objMemberName As IXMLDOMElement
Set objDom = New DOMDocument
objDom.appendChild objDom.createProcessingInstruction("xml","version='1.0' encoding='UTF-8' standalOne=""yes""")
Set objRootElem = objDom.createElement("_XPXML")
objDom.appendChild objRootElem
XMLFileName = "C:\Users\SHIVAMAIN\Documents\xml\TEST.xml"
Set ParentNode = objDom.SelectSingleNode("_XPXML")
Set childNode = objDom.createElement("_FTrans")
ParentNode.appendChild childNode
Set nodeAttribute = objDom.createAttribute("Unit")
nodeAttribute.NodeValue = 2
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("UnitAmount")
nodeAttribute.NodeValue = 525.3
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("RefN")
nodeAttribute.NodeValue = 0
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Desc")
nodeAttribute.NodeValue = "ÈÇÈÊ ÝǘÊæÑ ÝÑæÔ1"
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Cre")
nodeAttribute.NodeValue = 0
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Deb")
nodeAttribute.NodeValue = 3798385
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Det2")
nodeAttribute.NodeValue = "16/0002"
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Det1")
nodeAttribute.NodeValue = "03/0654"
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("acc")
nodeAttribute.NodeValue = "1/0/01/02"
childNode.setattributeNode nodeAttribute
Set childNode = objDom.createElement("_FTrans")
ParentNode.appendChild childNode
Set nodeAttribute = objDom.createAttribute("Unit")
nodeAttribute.NodeValue = 2
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("UnitAmount")
nodeAttribute.NodeValue = 54.6
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("RefN")
nodeAttribute.NodeValue = 645000
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("ODesc")
nodeAttribute.NodeValue = 41970130298#
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Desc")
nodeAttribute.NodeValue = "ÈÇÈÊ ÝǘÊæÑ ÝÑæÔ"
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Cre")
nodeAttribute.NodeValue = 352170
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Deb")
nodeAttribute.NodeValue = 0
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Det2")
nodeAttribute.NodeValue = "03/0654"
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("Det1")
nodeAttribute.NodeValue = "09/1571"
childNode.setattributeNode nodeAttribute
Set nodeAttribute = objDom.createAttribute("acc")
nodeAttribute.NodeValue = "4/0/00/01"
childNode.setattributeNode nodeAttribute
objDom.Save (XMLFileName)
End Function
![使用VBA编辑XML rootelement 使用VBA编辑XML rootelement](https://img.php1.cn/3cd4a/1eebe/cd5/1e3db12dd78db092.webp)
在创建根元素之后仅使用setAttribute
属性。在下面的示例中,相应地填写...
。
...
' ROOT ELEMENT
Set objRootElem = objDom.createElement("_XPXML")
With objRootElem
.setAttribute "Desc",""
.setAttribute "AppId","1"
.setAttribute "Ver","802"
.setAttribute "Srv","SHN_ACCSERVER"
.setAttribute "DBN","_AccXP_SHN98"
End With
objDom.appendChild objRootElem
' ROOT CHILD NODES
Set childNode = objDom.createElement("_FTrans")
With childNode
.setAttribute "Unit","2"
.setAttribute "UnitAmount","54.6"
.setAttribute "RefN","645000"
...
End With
objRootElem.appendChild
...