Say I have a multiple files which look like follows, and I'm looping through storing them in a variable called 'text':
<Property name="FirstProp"><![CDATA[ ]]></PackageProperty>
<Property name="SecondProp"><![CDATA[ ]]></PackageProperty>
<Property name="ThirdProp"><![CDATA[ ]]></PackageProperty>
Some of the files can have CDATA in them, so the only thing each has in common is the property name. I've stored the PropertyNames in a String array called propNames[ ]. So, "SecondProp" would be propNames[1].
I now want to replace the second line to include some CDATA, I've tried this but it doesn't work because I only want it to look before and after the current line, not the entire file.
String CDATAReplacement = "<Property name=\"SecondProp\"><![CDATA[ Some Value ]]></PackageProperty>";
text.replaceAll( "(.*)"+propNames[1]+"(.*)", CDATAReplacement )
End result:
<Property name="FirstProp"><![CDATA[ ]]></PackageProperty>
<Property name="SecondProp"><![CDATA[ Some Value ]]></PackageProperty>
<Property name="ThirdProp"><![CDATA[ ]]></PackageProperty>
I could do this quite easily using sed and execute the script within the java program, but I know it's bad practise so a Java solution would be better.