Editing the content within tags in XML file

I have an XML file I would like to edit. There are just three of four kinds of tags with numerical values, like

<tag1>124.23</tag1>
<tag2>5643.45</tag2>
<tag3>765.12</tag3>
<tag4>12.44</tag4>

I would like to search and replace just the contents of tag2 and tag3 and replace them with 0.00, but leave the contents of tag3 and tag4 alone

How can I do this? I have Notepad++ if that helps

Notepad++ supports regular expressions in its find dialog (if you select the option in the lower left corner of that dialog). Can you search for “<tag2>.*</tag2>” ?

That needs to be non-greedy or you’re in for a world of hurt. Make it .*?

But parsing XML with regexes is dangerous to begin with.

(Note: I assume there’s a mistake in your OP and that you want to replace tags 1 and 2 but keep 3 and 4?)

If it’s actually valid XML (not just something that looks like it), you can also use the XSLT language to do this. For example, go to http://www.freeformatter.com/xsl-transformer.html#ad-output

In the first box, upload your XML or use this sample:



<root>
<tag1>124.23</tag1>
<tag2>5643.45</tag2>
<tag3>765.12</tag3>
<tag4>12.44</tag4>
</root>


In the second box, the XSLT you need is:



<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="pReplacement" select="'0.00'"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="tag1/text()|tag2/text()">
  <xsl:value-of select="$pReplacement"/>
 </xsl:template>
</xsl:stylesheet>


Near the bottom, you can see where it says tag1/text() | tax2/text(). You can modify the tag1 and tag2 parts to something else if those are wrong.

The output becomes:



<?xml version="1.0" encoding="UTF-8"?>
<root>
   <tag1>0.00</tag1>
   <tag2>0.00</tag2>
   <tag3>765.12</tag3>
   <tag4>12.44</tag4>
</root>


The advantage of using XSLT over regex is that it’s 1) more powerful and 2) aware of the XML hierarchy, having been designed specifically to manipulate XML. The disadvantage is that it’s got quite a learning curve (even more than regex) and its syntax isn’t much clearer. If it’s just a one-off job, it’s probably not worth it. If you’re going to keep doing it, though, it might be worth learning.

Thanks guys! Regex worked like a charm!