0

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?

ryekayo
  • 2,341
  • 3
  • 23
  • 51
  • I did that.. that didnt make a difference.. – ryekayo Oct 01 '15 at 14:50
  • I see the problem. You are updating A.get.... element, but writing "document" to the file. Instead of updating call getChild on "document" and write the document to file. – kosa Oct 01 '15 at 14:52
  • @Nambari, ooh Im glad you caught that? Can you provide an answer as to what exactly what I should do.. Sorry i am still learning how to use JDOM's methods... – ryekayo Oct 01 '15 at 14:54
  • Update "document" child, something like document.getChild.... (or) Write element to file like newDoc.output(a,......) – kosa Oct 01 '15 at 14:56
  • @Nambari, thanks for that.. I completely overlooked adding the variable a into the parameter for output(), can you create an answer with what you explained :) – ryekayo Oct 01 '15 at 15:25
  • @Nambari, actually now that I am running it, it is only replacing some instances of the String and not all.... – ryekayo Oct 01 '15 at 15:30
  • "replacing some instances" --> is a clue that you might be operating on only certain child of root elements. First, I would try to find out why only certain childs. – kosa Oct 01 '15 at 15:33
  • Just realized that right now actually lol – ryekayo Oct 01 '15 at 15:35
  • Glad you got it! Good luck! – kosa Oct 01 '15 at 15:36
  • Yeah just trying to figure out what Element to add since the other elements I tried keep giving me an NPE – ryekayo Oct 01 '15 at 15:44

1 Answers1

2

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,......)

kosa
  • 65,990
  • 13
  • 130
  • 167
  • I hate to ask, but would you be able to tell based on my code what to feed for the second parameter for findVirtue(String, Element)? The code is not writing all the information to the new xml docs I created... – ryekayo Oct 01 '15 at 15:51
  • I can't exactly tell, but my guess is you have redundant code here. 1) Whoever calling findVirtue(...) pass an element from XML and you need to operate on that. If that is not the case, then 2) Second parameter is useless, just remove and operate on the document instead of a.getchild... – kosa Oct 01 '15 at 15:54