I have a method that parses for a specific String and is supposed to change that String using JDOM's setText() method. But when I look at the document after the program finishes, the String is never changed:
public void findVirtue(String word, Element a) throws JDOMException,
IOException, TransformerFactoryConfigurationError,
TransformerException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(xml);
Pattern p = Pattern.compile("(?i)\\bvirtue('?s)?\\b",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(word);
List<String> virtue = new ArrayList<String>();
// Need to Increment a counter
while (m.find()) {
virtue.add(m.group());
log("FOUND : " + m.group());
log("REPLACEMENT : " + m.replaceAll("Ryan"));
a.getChild("LINE").setText(m.replaceAll("Ryan"));
XMLOutputter newDoc = new XMLOutputter();
newDoc.setFormat(Format.getPrettyFormat());
newDoc.output(document, new FileWriter(
"C:\\Users\\Ryan\\workspace\\Tragic\\result" + xml.getName()));
//counter();
}
}
Now I know it is not a problem with the regex since I am able to log the output to another file. But when I use:
a.getChild("LINE").setText(m.replaceAll("Ryan"));
It never works. Can someone please tell me what I am doing wrong?
I see the problem. You are updating a.getChild("LINE")
element, but writing document to the file.
How to solve this?
Update document
object child, something like document.getChild("LINE")
(or) write element to file like newDoc.output(a,......)