VBScript Find a node in XML node and replace the value - vbscript

How to write a vbscript which should search for a specific node in a XML file and replace the value of that node with another value?
So far I can read a node and get the value.
set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = "false"
objXML.load("E:\sage2\test.xml")
Set Root = objXML.documentElement
For Each x In Root.childNodes
if x.nodename="showList" then
plot=x.text
msgbox plot
end if
Next
please suggest me some example which should read a specific node in the xml file and replace the value of that node.

Here is a simple XML edit and save example in VBScript. I recommend lokking into using Xpath to select your node instead of looping over the child nodes, you can provide your XML for more detailed answer.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.load "MYFILE.xml"
'Locate the desired node
'Note the use of XPATH instead of looping over all the child nodes
Set nNode = xmlDoc.selectsinglenode ("//parentnode/targetnode")
'Set the node text with the new value
nNode.text = "NEW VALUE"
'Save the xml document with the new settings.
strResult = xmldoc.save("MYFILE.xml")

Related

Dynamically Rename Shared OR object names

As per the naming convention we are following, we need to rename every object to its standard name.
One such convention is to replace space between with ‘_’
eg. Object name ->Object_name
Is there any way to perform it dynamically using lines of code.?
What you can do , Export the repository to the XML . Then Using the XML dom Object you can navigate to each Node.Each Node will have a Name Attribute .Then you can check there is a space if it is You can change the logical name of it .This will change the Object Repository Names .
But you need do similar king of change in your QTPscript to get reflected .
Export the OR to xml file and use the following line of code.
And use the xml generated to import OR back to QTP.
This is specific to SAP GUI
Function ModifyORXML(inputFilepath,outputFilepath)
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load(inputFilepath)
Set xmlNodeList = xmlDoc.getElementsByTagName("qtpRep:Object")
num = xmlNodeList.length
For each x in xmlNodeList
AttName=x.getattribute("Name")
If x.getattribute("Class")="SAPGuiButton" Then
tmp=Split(AttName," ",-1,1)
AttName=tmp(0)
End If
AttName=Replace(AttName,Chr(34)," ")
AttName=Replace(AttName,")"," ")
AttName=Trim(AttName)
oldAttName=AttName
AttName=Replace(AttName,":"," ")
AttName=Trim(AttName)
AttName=Replace(AttName," ","_")
AttName=Replace(AttName," __","_",1,-1,1)
x.Attributes.getNamedItem("Name").Text = AttName
Next
xmlDoc.Save outputFilepath
End Function

Filter records from xml by date

I want get records from 2014-07-15T00:00:00-05:00 date from below xml. how can i achive this using vbscript? currently i am using this but is not working.
Set colNodes=xmlDoc.selectNodes _
'("//xyz/abc[date>'2014-07-14T00:00:00-05:00']")
<xyz>
<abc>
<date>2014-07-14T00:00:00-05:00</date>
<text>test1</text>
</abc>
<abc>
<date>2014-07-14T00:00:00-05:00</date>
<text>test2</text>
</abc>
<abc>
<date>2014-07-15T00:00:00-05:00</date>
<text>test3</text>
</abc>
<abc>
<date>2014-07-15T00:00:00-05:00</date>
<text>test4</text>
</abc>
<abc>
<date>2014-07-15T00:00:00-05:00</date>
<text>test5</text>
</abc>
</xyz>
Taking a lead from an answer to a related question, I tried this and it seemed to work:
Option Explicit
dim xml: xml = "<xyz><abc><date>2014-07-14T00:00:00-05:00</date><text>test1</text></abc><abc><date>2014-07-14T00:00:00-05:00</date><text>test2</text></abc><abc><date>2014-07-15T00:00:00-05:00</date><text>test3</text></abc><abc><date>2014-07-15T00:00:00-05:00</date><text>test4</text></abc><abc><date>2014-07-15T00:00:00-05:00</date><text>test5</text></abc></xyz>"
'note version 6.0 of the DOM Document
dim xmldoc: set xmldoc = CreateObject("MSXML2.DomDocument.6.0")
xmldoc.async = false
' error check your XML, quit
if not xmldoc.loadXML(xml) then
WScript.Echo xmldoc.parseError.reason
WScript.Quit 1
end if
dim nodes: set nodes = xmldoc.selectNodes("//xyz/abc[number(translate(date, '-.:T', ''))>number(translate('2014-07-14T00:00:00-05:00', '-.:T', ''))]")
' put results into fragment because we won't have a full valid XML document
dim frag: set frag = xmldoc.createDocumentFragment()
dim node
for each node in nodes
frag.appendChild node
next 'node
' use result
WScript.Echo frag.xml
The trick is realising that XPath and XSL 1.0 do not seem to support DateTime types, so to run a numeric comparison we strip any punctuation from the data values using translate() before converting to a number.
One issue with this is it does not take into account your timezone component so you may get incorrect results if dates differ purely by timezone, for example. This is a starting point at least.

How to read and write non-standard document properties of word file in vbscript?

Microsoft Word is offering some default document properties to be set in Word documents.
There is a number of default properties, for which vbscript has constants.
But Word (2011) is offering some more properties, e.g. companyfaxnumber, publishingdate,keywords.
There is a possibility to access the builtin properties by calling
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open(strFilePath)
For Each prop In oWord.ActiveDocument.BuiltInDocumentProperties
WScript.Echo prop.Name + "::" + oWord.ActiveDocument.BuiltInDocumentProperties(prop.Name).Value
Next
But how do i find the names of the "custom" properties that are offered by word, but are not present in vbscript as constant?
There is the function
Document.CustomDocumentProperties
But if i do a listing like the one above, i get properties named info1, info2, etc.
Too access the Word CustomDocumentProperties, you will need to be able to access the OLE File Property Reader. This expands beyond the normal/simple document properties because it allows you too add custom properties as well.
There is a Tales from the Script article from 2005 detailing the installation and usage of utilizing CustomDocumentProperties within Word -> Here
For the download to install the OLE Property Reader DLL, Go -> Here
Here is an example of property set/get once the property read is installed:
Const msoPropertyTypeBoolean = 2
Set objFile = CreateObject("DSOFile.OleDocumentProperties")
objFile.Open("C:\Scripts\New_users.xls")
'Set
'=======================================================================
objFile.CustomProperties.Add "Document Reviewed", msoPropertyTypeBoolean
objFile.Save
'Get
'=======================================================================
Set objProperty = objFile.CustomProperties.Item("Document Reviewed")
wscript.echo objProperty.Value
Enjoy!
Hi recently figured out how to get there myself:
The Word "Frontend Editor" is cheating on the document properties. There is a hard defined set of properties like author,category, keywords etc.
The additional properties offered by the editor are so called custom properties which are defined in an external XML structure inside the docx-container.
So there is no easy vbscript function to modify the values of these custom properties.
Thanks to the web, someone did some hacking and this is the solution for it:
Sub WriteCustomCoverProperties(ByRef wordInstance, strProp, strText)
Dim oCustPart
Dim oNode
Dim strXPath
strProp = Replace(strProp, " ", "")
Select Case strProp
Case "Abstract" strXPath = "/ns0:CoverPageProperties[1]/ns0:Abstract[1]"
Case "PublishDate" strXPath = "/ns0:CoverPageProperties[1]/ns0:PublishDate[1]"
Case "CompanyAddress" strXPath = "/ns0:CoverPageProperties[1]/ns0:CompanyAddress[1]"
Case "CompanyPhone" strXPath = "/ns0:CoverPageProperties[1]/ns0:CompanyPhone[1]"
Case "CompanyFax" strXPath = "/ns0:CoverPageProperties[1]/ns0:CompanyFax[1]"
Case "CompanyEmail" strXPath = "/ns0:CoverPageProperties[1]/ns0:CompanyEmail[1]"
Case Else
Exit Sub
End Select
Set oCustPart = wordInstance.ActiveDocument.CustomXMLParts(3)
Set oNode = oCustPart.SelectSingleNode(strXPath)
oNode.Text = strText
Set oCustPart = Nothing
Set oNode = Nothing
End Sub
May it be of help =)

Compare two xml files using VBS in QTP

I need to compare 2 xml files using QTP where the values for each tag needs to be compared and need to print the difference in values if found. I used the built in Function XMLUTIL but its not working as expected i.e.. its creates a file with differences including the parent tag.
<tns:AAL_Request_NEW xsi:schemaLocation="http://www.bnymellon.com/AAL_Request_NEW AAL_Request_NEW.xsd">
<tns:OPF_Information>
<tns:Source>
<tns:Source>EPH</tns:Source>
</tns:Source>
<tns:References>
<tns:Accounting_Record_Reference>130830000672401</tns:Accounting_Record_Reference>
<tns:OPF_Reference>EPH1308300006724</tns:OPF_Reference>
<tns:Group_Reference>EPH1308300006723</tns:Group_Reference>
</tns:References>
</tns:OPF_Information>
</tns:AAL_Request_NEW>
In the above xml file i just need the tags with values like
tns:Source with value EPH, tns:Accounting_Record_Reference with value 130830000672401, tns:OPF_Reference with value EPH1308300006724 and tns:Group_Reference EPH1308300006723 to be compared and not the parent tags like tns:References, tns:OPF_Information or tns:AAL_Request_NEW.
Can anyone help with the logic to fetch the tags which has no child tag inside it and ends immediately with having only a value between its start <> and end and compare it with the other file and print the tag name and the values if there is a difference?
you can use CreateObject("Microsoft.XMLDOM") to read the xml files and retrive the tags by tag name and comparte them both.
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("<XML PATH>")
Set Root = objXMLDoc.documentElement
Set tags = root.tagnames
Set NodeList = Root.getElementsByTagname("<node name>")
For Each Elem In NodeList
msgbox Elem.text
Next
Thanks and regards

VBScript: XPath Query with multiple namespaces

What XPath query should I use to get to the GetLogisticsOfferDateResult Node?
I have attached the vbscript that I'm using.
I suspect the problem has to do with the multiple namespaces in the document. But how do I reference the second namespace in the XPath?
Dim responseXML
responseXML = '"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""><s:Body><GetLogisticsOfferDateResponse xmlns=""http://schneider-electric.com/OrderEntryService""><GetLogisticsOfferDateResult>2010-07-20</GetLogisticsOfferDateResult></GetLogisticsOfferDateResponse></s:Body></s:Envelope>"'
Dim responseDoc
Set responseDoc = WScript.CreateObject("MSXML2.DOMDocument.6.0")
responseDoc.loadXML(responseXML)
responseDoc.setProperty "SelectionNamespaces", "xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'"
Dim requestedNode
'This node is not found
'Set requestedNode = responseDoc.selectSingleNode("//s:Envelope//s:Body//GetLogisticsOfferDateResponse//GetLogisticsOfferDateResult")
'This node is found
Set requestedNode = responseDoc.selectSingleNode("//s:Envelope//s:Body")
'This node is found
'Set requestedNode = responseDoc.selectSingleNode("//s:Envelope")
If requestedNode Is Nothing Then
WScript.Echo "Node not found"
Else
WScript.Echo requestedNode.text
End If
Set responseDoc = Nothing
Set LODateNode = Nothing
Turns out my setting of selectionNamespaces had to be as follows:
responseDoc.setProperty "SelectionNamespaces", "xmlns:sc='http://schneider-electric.com/OrderEntryService' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'"
Then the XPath query had to be:
Set requestedNode = responseDoc.selectSingleNode("//s:Envelope//s:Body//sc:GetLogisticsOfferDateResponse//sc:GetLogisticsOfferDateResult")
You have not defined the default namespace of the document (http://schneider-electric.com/OrderEntryService) in your code.
responseDoc.setProperty "SelectionNamespaces", "'http://schemas.xmlsoap.org/soap/envelope/' 'http://schneider-electric.com/OrderEntryService'"
You will either need to add it, or prefix the elements that belong to it with it.

Resources