Solved

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

Posted on 2015-01-22
3
48 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 26

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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now