I have an XML file that I'm trying to call the String replace() method on it's content after I read the content into a String Object. I tried this step a few different ways and some have been cleaner than others, but I don't think this step is causing the problem. The step seems to be occurring with the actual replace call. I'm iterating through a list of key/value pairs and replacing every key with the corresponding value, which works, but when it's complete the XML file is missing random text throughout the file. The crazy part is that the text that is missing, isn't anything that i'm trying to replace. I've been able to isolate a list of Strings/text that are being removed from the final result, the list is as follows:
BSB
BSTB
MI
ABCT
Distribution
Capture
Manag
Please HELP!!! I've been looking at this for two days now.
I apologize if this is in the wrong thread but I'm new to the forum and wasn't sure where to post.
Here is the method that i'm working with
public static void findAndReplace(String goodXMLPath) { // Local Variables Path path; Charset charset = StandardCharsets.UTF_16; String content, find, replace; // Initialize path = Paths.get(goodXMLPath); try { content = new String(Files.readAllBytes(path), charset); logger.debug("BEFORE: " + content); for (Definition d : definitionsToReplace) { find = d.getGUID().replace("\"", ""); replace = d.getKeyGUID().replace("\"", ""); if (content.contains(find) || content.indexOf(find) != -1) content = content.replace(find, replace); else logger.debug("Didn't find string: " + find); } logger.debug("AFTER: " + content); Files.write(path, content.getBytes(charset)); } catch (IOException e) { e.printStackTrace(); } }