Replace different values from the xml tags with one value in all the open documents.
-
Replace different values from the xml tags with one value in all the open documents.
I have around 30-40 xml files for each soap testcase and the xml files have the below tag with this random data.
<regUID>76D89A01-1326-4A0A-9D2B-69C9A3D570AE</regUID> (file 1)
<regUID>96E197E2-05CE-47CC-AA41-1FBB0A019A5E</regUID> (file 2)I need to make the above tag as
<regUID>${=java.util.UID.randomUID()}</regUID>So that the value inside the regUID tag is set as dynamic and it should be replaced to the above value in all the open xml documents.
I cannot perform the random operation of find and replace in all open documents because there is no constant value that can be found.
Guys, if you can help, it will be really appreciated.
Thanks in advance.
-
@savio-shaji said in Replace different values from the xml tags with one value in all the open documents.:
<regUID>${=java.util.UID.randomUID()}</regUID>
I will assume you want that literal text as the replacement to go in the XML file, and that your Java will take care of the “dynamic” at a later time.
If that’s the case, then it’s going to be really easy, using Notepad++'s “Regular Expression” search mode (also known as “regex”).
The most straightforward approach is to just look for any text between the opening and closing
regUID
tags:- Find What =
(?-s)<regUID>(.*?)</regUID>
- Replace With =
<regUID>${=java.util.UID.randomUID\(\)}</regUID>
- Search Mode = Regular Expression
- Replace All
However, that has the problem that if you have
<regUID>Invalid, so don't put replacement here</regUID>
it would match, which you might not want.
If you want to ensure that there’s only UUID data between the tags, you can use character classes to limit what’s allowed between the tags.
- Find What =
(?-s)<regUID>([[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12})</regUID>
- Replace With =
<regUID>${=java.util.UID.randomUID\(\)}</regUID>
- Search Mode = Regular Expression
- Replace All
<edit> PS: had to add
\
before the(
and)
in the replacement, because parens are still special in replacement syntax. </edit>----
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the
</>
toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipboard to your post usingCtrl+V
to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. - Find What =
-
And of course, to augment @PeterJones 's reply, because
@savio-shaji said:
…should be replaced … in all the open xml documents.
You’ll probably want the Replace All in All Opened Documents button.
But you can’t limit that to only xml docs, so probably want to close others first if the potential for hits in non-xml files exists.
-
or … like this
Find What =>.+<
Replace With =>${=java.util.UID.randomUID\(\)}<
-
@Olivier-Thomas said in Replace different values from the xml tags with one value in all the open documents.:
or … like this
Find What =>.+<
Replace With =>${=java.util.UID.randomUID\(\)}<
Be wary when following that advice. It will work – if the only sequence of
>
followed by any text followed by<
just happens to be your<regUID>
pair. If you have any tags you don’t want to replace (if there is any other<tag>...</tag>
sequence in your data than the<regUID>...</regUID>
sequence), it would be a bad idea to try @Olivier-Thomas’s regex, because it would change the contents of every single tag in your XML file. Please be wary. I made my regex more specific, only changing things inside<regUID>
pairs, because I didn’t want to assume that your data only had<regUID>
tags and no other tags. -
Agreed.
Often people try to be “impressive” by coming up with a shorter regex.
This is best-left to the “pros”, if it even needs to be done at all.
I’m a believer in NOT doing this kind of thing.
If a solution works, it doesn’t have to be short or even efficient.
To bear repeating: This isn’t a regex site (where such things might excite the participants). -
And I assumed his data only has <regUID> tags as seen in the example.
-
@Olivier-Thomas said in Replace different values from the xml tags with one value in all the open documents.:
And I assumed his data only has <regUID> tags as seen in the example.
It’s tough to make that assumption, when so little data is provided.
What people do here, when little data is provided, is to “assume the typical” (or ask for more info).
“Typical” for a “file with tags” is that there are many, different types of these.
What this can do is avoid the frequently observed next exchange: “But my data has many types of tags, and your solution doesn’t handle that…”
-
Thank you @PeterJones @Alan-Kilborn @Olivier-Thomas
First of all, this is my first ever post on community platform asking solution to a doubt. Never expected any reply to this post.
But I received suggestions and i’m grateful for that.Yes, I provided little information on the query. Apologies for that.
The xml files I’m talking about is SOAP request XML’s with about 100-150 lines of data.I tried out both the suggestions by @PeterJones and @Olivier-Thomas and it worked.
Find What = (?-s)<regUID>(.*?)</regUID>
Replace With = <regUID>${=java.util.UID.randomUID()}</regUID>Find What = <regUID>.+</regUID>
Replace With = <regUID>${=java.util.UID.randomUID()}</regUID>Thank you all of you for providing help!
-
Replace With =
>${=java.util.UID.randomUID\(\)}<
@Alan-Kilborn
It’s tough to make that assumption, when so little data is provided.You’re right.
What is an example, such an answer