0

I am new to XML. I use the following function to create an xml file. I want change the code to add some more details to its root element. you can refer to the attached picture for my purpose.

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

enter image description here

1 Answer 1

2

Simply use the setAttribute property after creating root element. Fill in ... accordingly in below example.

...
' 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
...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.