Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 71
  • Last Modified:

Need assistance with creating a VB script that can replace text between two file strings. XML file

Hello

Need assistance with creating a VB script that can replace text between two file strings.  We have a script now that replaces string directly and works well . Will place sample below. We have a need to replace or remove text data betewen to string values (XML tags). The file is a actually an xml file. We have hundreds of xml files in specific folders that need to have the specific string replaced. Posting sample XML below.

If someone has a better way to replace values would like to see.  The goal is to replace the data between the two XML tags for a given data element. I have been lucky to get away with just the string replacements but now have a need to remove sting values between the elements. For example need to replace data between the  <PROJECT_UID>898765543456789</PROJECT_UID> data fields so set can reset ID.

I am trying to keep the script simple so that can use load runner tool to run so we can take advantage of the parameter capabilities it has.
Here is a script now that runs well in Load runner scripting tool replacing string values for the specific files. Wonder if ways to use expression or pattern. The problem is the data varies between fields varies in length. Using a tool like Notpad ++ works for single replacments like exspresion <PROJECT_UID>.*</PROJECT_UID>. It finds value then we replace of remove for reset.

Need the code simple and flat so can run through loadrunner tool. Here is sample.

Public Function Action()

    '" TO DO: Place your action code here

Const ForReading = 1
Const ForWriting = 2

'"FolderPathOld = lr.eval_string("FPathOld")
'"FolderPathNew = lr.eval_string("FPathNew")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(lr.eval_string("<PathDir>"), ForReading)

strText = objFile.ReadAll
objFile.Close

'"strNewText = Replace(strText, "//CCCCCCCC\CCCC:5555", "//CCCCCCCC\CCCC:5555")
strNewText = Replace(strText, lr.eval_string("<PathOld>"), lr.eval_string("<PathNew>"))

Set objFile = objFSO.OpenTextFile(lr.eval_string("<PathDir>"), ForWriting)
objFile.WriteLine strNewText
objFile.Close

lr.output_message(FolderSize & "," & lr.eval_string("<PathOld>"))
lr.output_message(FolderSize & "," & lr.eval_string("<PathNew>"))

    Action = lr.PASS
End Function


Sample XML
==========
<?xml version="1.0" encoding="UTF-8"?>
<ProjectDescription>
      <PROJECT_NAME>123510</PROJECT_NAME>
      <DB_TYPE>2</DB_TYPE>
      <DESCRIPTION>Created on 2014-11-12 15:06:07 Project End Date Oct-14 2013</DESCRIPTION>
      <DB_CONNSTR_FORMAT>jdbc:mercury:sqlserver://server1\inst1:8888</DB_CONNSTR_FORMAT>
      <DB_NATIVE_AUTHENTICATION>Y</DB_NATIVE_AUTHENTICATION>
      <DB_NAME>PROGCT1</DB_NAME>
      <DBSERVER_NAME> server1\inst1</DBSERVER_NAME>
      <DB_USER_PASS>5434tr5678*&^%$</DB_USER_PASS>
      <PR_HAS_VCSDB>N</PR_HAS_VCSDB>
      <PHYSICAL_DIRECTORY>\\server1234:8888\g$\test1\</PHYSICAL_DIRECTORY>
      <USERS_QUOTA>-1</USERS_QUOTA>
      <PR_IS_ACTIVE>Y</PR_IS_ACTIVE>
      <SAQ_IS_ACTIVE>N</SAQ_IS_ACTIVE>
      <VM_REPOSITORY></VM_REPOSITORY>
      <PR_LANGUAGE>English</PR_LANGUAGE>
      <PROJECT_TYPE>Standard</PROJECT_TYPE>
      <IS_TEMPLATE>N</IS_TEMPLATE>
      <PROJECT_UID>898765543456789</PROJECT_UID>
      <PR_SMART_REPOSITORY_ENABLED>Y</PR_SMART_REPOSITORY_ENABLED>
      <PR_IS_QPM_AUTO_CALC_ENABLED>Y</PR_IS_QPM_AUTO_CALC_ENABLED>
</ProjectDescription>
0
williamfl
Asked:
williamfl
  • 2
1 Solution
 
MacroShadowCommented:
You can use this in Notepad++, just remember to tick the regular expression checkbox:
(<PROJECT_UID>).*(</PROJECT_UID>)

Open in new window

If you still want a VB Script post back.
0
 
williamflAuthor Commented:
Yes, that works for one at a time but i have thousands that need to be changed quickley. So i need to bulk script it. Do not think we can line command notepad ++.
0
 
MacroShadowCommented:
I'm not familiar with the Load runner scripting tool. But this is how I would replace the value of an xml node.
Set objxmlDoc = CreateObject("Microsoft.XMLDOM")
'Load the xml document
objxmlDoc.load "C:\Temp\XmlFiles\Xml1.xml"
'Locate the desired node
Set nNode = objxmlDoc.SelectSingleNode ("//ParentNode/ChildNode/TargetNode") 
'Set the new value
nNode.text = "whatever"
'Save the modified xml document
objxmlDoc.Save("C:\Temp\XmlFiles\Xml1.xml")

Open in new window


How exactly to loop thru the files and nodes to replace, depend on your setup. If you provide the following info I'll give a more detailed answer.
1. Does each file have it's own list of nodes/values to be replaced or do they all have the same changes required?
2. Can two files have the same node but have to be changed to a different value?
3. Would you rather store the node list in an external file or hard code them?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now