Changing node values in XML using VBS with similar names - vbscript

I am facing this challenge with changing values of nodes in an xml file which has same names, using VBScript. Following is the sample XML:
- <MappingData>
<Name>Name 1</Name>
- <ValueField FieldName="Name 2">
<CharValue>Value 1</CharValue>
</ValueField>
- <ValueField FieldName="Name 3">
<CharValue>Value 1</CharValue>
</ValueField>
My requirement is to change the values of both occurrences of tag
<CharValue>
.
I could have done this if there was any attribute in these tags, but in this case I am stuck.
I tried the following code, but could'nt get what I need.
Set NodeList = objXMLDoc.documentElement.selectNodes("//MappingData/ValueField/CharValue")
For i = 0 To NodeList.length - 1
node.Text = "Value 1"
Next
Any help is appreciated. Thanks.

Try the following code
Set xDoc = CreateObject( "Msxml2.DOMDocument.6.0" )
xDoc.setProperty "SelectionLanguage", "XPath"
xDoc.async = False
xDoc.load "test.xml"
Set nNodes = xDoc.selectNodes("//MappingData/ValueField/CharValue")
For i = 0 To nNodes.Length - 1
nNodes(i).text = "Changed value " & i
Next
xDoc.Save "test.xml"

Related

How do I reach the second child node of an element XML in vbscript?

After spending many hours on the internet I was not able to get this to work and need some help.
XML:
xml snippet highlighting NoteSynopsis
CODE:
set xmlDoc = SERVER.CREATEOBJECT("MSXML2.DomDocument.6.0")
xmlDoc.async = False xmlDoc.Load("D:\GVPApplications\TechSupportCreateIncident\CreateIncidentRequest.xml")
xmlDoc.setProperty "NewParser","true"
set Root = xmlDoc.documentElement
set NodeList1 = Root.getElementsByTagName("IncidentNote")
For Each Elem in NodeList1
if Elem.firstChild.nodename = "Note" then
Elem.firstChild.text = Notes
end if
Next
ISSUE:
As highlighted in the image XML , I need to be able to read "NoteSynopsis" element and its value. As simple as this seems I have not been able to find a solution to this. If this was the last child no issue, I would do a Elem.LastChild.nodename, but this isn't!
You could use the selectSingleNode(...) method and use XPath
set xmlDoc = SERVER.CREATEOBJECT("MSXML2.DomDocument.6.0")
xmlDoc.async = False xmlDoc.Load("D:\GVPApplications\TechSupportCreateIncident\CreateIncidentRequest.xml")
xmlDoc.setProperty "NewParser","true"
xmlDocument.setProperty "SelectionLanguage", "XPath"
Dim firstIncidentNote
set firstIncidentNote = xmlDoc.SelectSingleNode("//IncidentNote")
Dim note
Dim noteSynopsis
set note = firstIncidentNote.GetAttribute("Note")
set note = firstIncidentNote.GetAttribute("NoteSynopsis")
https://msdn.microsoft.com/en-us/library/ms757846(v=vs.85).aspx

Convert vbscript into rapid integration flowlanguage

I'm using Pervasive 9.2 and would like to somehow convert the vbscript into pervasives RIFL language. I want to execute the code during a Process where the f(x) component is executed to run the code. As far as the Path to the xml data, I'll be using a Macro defining where the source data is and another to save the file as well.
Here is the vbscript code below:
Set xml = CreateObject("Microsoft.XMLDOM")
xml.async = False
count_var = 1
If xml.Load("c:\folder1\test.xml") Then
For Each accounts In xml.SelectNodes("//Accounts")
For Each account In Accounts.SelectNodes("./Account")
If count_var > 1 Then
Set accountEnum = xml.createNode(1, "Account" & count_var, "")
For Each child In Account.childNodes
accountEnum.appendChild(child.cloneNode(TRUE))
Next
Accounts.replaceChild accountEnum, Account
xml.save("c:\folder1\test.xml")
else
Set accountEnum = xml.createNode(1, "Account" & count_var, "")
For Each child In Account.childNodes
accountEnum.appendChild(child.cloneNode(TRUE))
Next
Accounts.replaceChild accountEnum, Account
xml.save("c:\folder1\test.xml")
End If
count_var = count_var + 1
Next
count_var = 1
Next
End If
Set node = Nothing
Set xml = Nothing
ParseXMLFile(filepath)
documentation here
http://docs.pervasive.com/products/integration/di/rifl/wwhelp/wwhimpl/common/html/wwhelp.htm#href=ParseXMLFile_Function.html&single=true
this will return a DOMDocument Object Type
documentation here
http://docs.pervasive.com/products/integration/di/rifl/wwhelp/wwhimpl/common/html/wwhelp.htm#href=DOMDocument_Object_Type.html&single=true
hope this helps

Object doesn't support this property or method: 'xmldoc2.importNode'

I am trying to copy nodes from one XML document into another.
Project is a root element inboth documents and I want to select all ItemGroup elements from first document and insert them before Import element in the second document. Unfortunately, I get
Object doesn't support this property or method: 'xmldoc2.importNode'
Here is the code I am using:
Set xmldoc1 = CreateObject("Microsoft.XMLDOM")
xmldoc1.async = false
xmldoc1.load WScript.Arguments(0)
Set xmldoc2 = CreateObject("Microsoft.XMLDOM")
xmldoc2.async = false
xmldoc2.load WScript.Arguments(1)
Set importNode = xmldoc2.selectSingleNode("//Project/Import")
Set nodes = xmldoc1.selectNodes("//Project/ItemGroup")
For Each node In nodes
Set newNode = xmldoc2.importNode(node, True)
xmldoc2.insertBefore newNode, importNode
Next
How should I fix the code?
EDIT:
Thanks to #Ekkehard.Horner, I solved the issue. Here is the updated code
Set xmldoc1 = CreateObject("Microsoft.XMLDOM")
xmldoc1.async = false
xmldoc1.load WScript.Arguments(0)
Set xmldoc2 = CreateObject("Microsoft.XMLDOM")
xmldoc2.async = false
xmldoc2.load WScript.Arguments(1)
Set importNode = xmldoc2.selectSingleNode("//Project/Import")
Set nodes = xmldoc1.selectNodes("//Project/ItemGroup")
For Each node In nodes
Set newNode = node.cloneNode(true)
xmldoc2.documentElement.insertBefore newNode, importNode
Next
The docs for importNode state:
[This sample code uses features that were first implemented in MSXML
5.0 for Microsoft Office Applications.]
I'd try to use
"Msxml2.DOMDocument" or "Msxml2.DOMDocument.6.0" instead of "Microsoft.XMLDOM"
.cloneNode instead of .importNode

Retrieve the value of a XML attribute in VBS

<Requirement Description="description" Operation="Configure">
<Action ID="1000" Name="Split">
<Contract>
<Parameter Name="Version">4</Parameter>
<Parameter Name="DefaultServer">192.168.00.</Parameter>
<Parameter Name="DefaultUser">administrator</Parameter>
<Parameter Name="DefaultPassword">password</Parameter>
<Parameter Name="DefaultDomain">192.168.00.00</Parameter>
<Parameter Name="Split">1</Parameter>
</Contract>
</Action>
</Requirement>
From the above XML document my aim is to replace the IP address for both the attributes default server and default domain from a VBScript.
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load(XMLFullPath)
Set NodeList = objXMLDoc.documentElement.SelectNodes("//Parameter")
NodeList(i).nodeName
Give name as Parameter and NodeList(i).Text gives me values like 4, IP address, administrator and others. But I am not able to get the attribute name so that I can directly change the value of the attribute.
To answer your question, you can use the getAttribute function to access an attribute's value:
NodeList(i).getAttribute("Name")
You can also add a predicate to the XPath expression in your SelectNodes call to retrieve only the desired elements:
Set NodeList = objXMLDoc.documentElement.SelectNodes("//Parameter[#Name = 'DefaultServer' or #Name = 'DefaultDomain']")
This way, you don't have to retrieve and loop through the Parameter nodes that you're not interested in.
A bit rusty, but I think you can use this to retrieve the nodevalue by nodename:
Function getTag(nList, nName)
Dim i
i = 0
Do while i < nList.childNodes.length
if (nList.childNodes(i).nodeName = nName) then
getTag = nList.childNodes(i).childNodes(0).text
Exit Function
end if
i = i + 1
Loop
End Function
And to set it, probably
Sub setTag(nList, nName, val)
Dim i
i = 0
Do while i < nList.childNodes.length
if (nList.childNodes(i).nodeName = nName) then
nList.childNodes(i).childNodes(0).text = val
Exit Sub
end if
i = i + 1
Loop
End Sub

How to save the active "Word" document in VBScript?

Here I have a small VBS script that helps me append a new line to a table in MS "Word" 2003:
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open ("c:\addtotable.doc")
Set r = doc.Tables(1).Rows.Add
aa = Split("turtle,dog,rooster,maple", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = aa(i)
Next
It works fine, but it doesn't save anything. I want it to save the performed changes.
By the method of macro-recording in the "Word" I got this macro command that saves active "Word" document:
ActiveDocument.Save
So, I decided to append this macro to the VBS script above:
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open ("c:\addtotable.doc")
Set r = doc.Tables(1).Rows.Add
aa = Split("turtle,dog,rooster,maple", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = aa(i)
Next
ActiveDocument.Save
But it doesn't save anything. What am I doing wrong here?
Have you already tried calling doc.Save after making those changes? If that doesn't work:
The issue is that ActiveDocument doesn't automatically reference what you think it does in VBScript the way it does in Word's VBA.
Try setting a new variable to the active document, like so:
Dim activeDoc
Set activeDoc = wd.ActiveDocument
activeDoc.Save
I think you have to use ActiveDocument.SaveAs("C:\addtotable.doc"); because I can't find any documentation for .Save. SaveAs accepts a second parameter which specifies what format to save it in. Pastebin of the parameters here.

Resources