Arun
Arun

Reputation: 23

Using VBScripting Add Node to a xml file

In the following XML file, using vbscript i want the add nodes

<?xml version="1.0" encoding="UTF-8"?>
<Project>
   <Variable name="Variable_1" />
   <Study>
      <Agent>
         <Variable name="Variable_1" baseline="0" distribution="" flags="2" max="1" min="0" />
      </Agent>
   </Study>
</Project>

After running the script, xml file will be like following.

<?xml version="1.0" encoding="UTF-8"?>
<Project>
   <Variable name="Variable_1" />
   <Variable name="Variable_2" />
   <Variable name="Variable_3" />
   <Study>
      <Agent>
         <Variable name="Variable_1" baseline="0" distribution="" flags="2" max="1" min="0" />
         <Variable name="Variable_2" baseline="4" distribution="" flags="2" max="3" min="1" />
      </Agent>
   </Study>
</Project>

I tried following codes but no results

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("us2.xml")
Dim objCurrNode, objNewNode, objNewText 
Set objRoot = xmlDoc.documentElement
Set objNewNode = XMLDoc.createElement("VarIable name")
Set objNewText = XMLDoc.createTextNode("Variable_2")
objNewNode.appendChild(objNewText)
xmlDoc.Save "Audits.xml"  

please suggest on this. Thanks.

Upvotes: 2

Views: 6386

Answers (2)

har07
har07

Reputation: 89285

Here is an example that demonstrates some technique not covered correctly in the code you tried, namely (null checking skipped) :

  1. Create attribute for an element,

  2. Add element to a parent node,

  3. Add element after certain existing element,

  4. Select particular element using Xpath and SelectSingleNode() method


Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("us2.xml")

Dim variable1, variable2, agent, variable2Agent
'select <Variable> node having name attribute equals Variable_1'
Set variable1 = xmlDoc.SelectSingleNode("/Project/Variable[@name='Variable_1']")
'create new <Variable>'
Set variable2 = xmlDoc.CreateElement("Variable");
'add name="Variable_2" attribute'
variable2.SetAttribute("name", "Variable_2");
'add new <Variable> after variable1 node'
variable1.ParentNode.InsertAfter(variable2, variable1);

'select <Agent> node'
Set agent = xmlDoc.SelectSingleNode("/Project/Study/Agent")
'create new <Variable> and set all required attributes'
Set variable2Agent = xmlDoc.CreateElement("Variable")
variable2Agent.SetAttribute("name", "Variable_2")
variable2Agent.SetAttribute("baseline", "4")
....
'add new <Variable> after the last child of <Agent> node'
agent.AppendChild(variable2Agent)

xmlDoc.Save "Audits.xml" 

Upvotes: 3

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

You can't createElements with attributes and attribute values aren't text children. Use XPath to find the model nodes, clone and modify them, and then appendChild them to the correct parent node. To get you started:

Option Explicit

Dim oXDoc  : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load "..\data\25604761.xml"

If 0 = oXDoc.ParseError Then
   WScript.Echo oXDoc.documentElement.xml
   Dim sXPath : sXPath     = "/Project/Variable"
   Dim ndlFnd : Set ndlFnd = oXDoc.selectNodes(sXPath)
   If 1 = ndlFnd.length Then
      Dim ndNew : Set ndNew = ndlFnd(0).cloneNode(True)
      ndNew.getAttributeNode("name").value = "Variable_2"
      ndlFnd(0).parentNode.appendChild ndNew
      WScript.Echo "----------------", vbCrLf & oXDoc.documentElement.xml
   Else
      WScript.Echo sXPath, "- not exactly one node found"
   End If
Else
   WScript.Echo oXDoc.ParseError.Reason
End If

output:

cscript 25604761.vbs
<Project>
        <Variable name="Variable_1"/>
        <Study>
                <Agent>
                        <Variable name="Variable_1" baseline="0" distribution="" flags="2"
                </Agent>
        </Study>
</Project>
----------------
<Project>
        <Variable name="Variable_1"/>
        <Study>
                <Agent>
                        <Variable name="Variable_1" baseline="0" distribution="" flags="2"
                </Agent>
        </Study>
        <Variable name="Variable_2"/>
</Project>

Upvotes: 0

Related Questions