Solved

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

Posted on 2015-01-22
3
60 Views
Last Modified: 2016-05-18
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
Comment
Question by:williamfl
  • 2
3 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40566053
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
 

Author Comment

by:williamfl
ID: 40566680
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
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40568628
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question