2014-09-29 2 views
3

следующий мой код Java для чтения XML-файла и обновления некоторых значений в нем.XML-файл не обновляется с помощью jdom

public static void writeLexicon(String word, String tag) { 
    int newFreq=0; 
    int tagAvailability = 0; 
    int wordAvaialbility = 0; 
    try { 
     if (new File("./src/Lexicon.xml").exists()) { 

      Document readDoc = getXMLFile(); 
      Element root = readDoc.getRootElement(); 
      for (Element curElem : root.getChildren("lexiconElement")) { 
       if (word.equals(curElem.getChildText("word"))) { // word avaialble 

        List<Element> subEle = curElem.getChildren(); 

        for (int i = 1; i < subEle.size(); i++) { 
         if (tag.equals(subEle.get(i).getChildText("tag"))) { 

          int curFreq = Integer.parseInt(subEle.get(i).getChildTextTrim("frequancy")); 
          newFreq = curFreq + 1; 
          subEle.get(i).getChild("frequancy").setText(String.valueOf(newFreq)); 
          tagAvailability = 1; 
          //break; 
         } 
        } 
        if (tagAvailability == 0) { 
         Element newTag = new Element("tag").setText(tag); 

         Element newFrequancy = new Element("frequancy").setText("1"); 
         newTag.addContent(newFrequancy); 
         curElem.addContent(newTag); 
        } 

        wordAvaialbility = 1; 
       } 


      } 
      if (wordAvaialbility == 0) { 
       Element lexiconElement = new Element("lexiconElement"); 
       Element newWord = new Element("word").setText(word); 

       Element newTag = new Element("tag").setText(tag); 

       Element newFrequancy = new Element("frequancy").setText("1"); 
       newTag.addContent(newFrequancy); 
       lexiconElement.addContent(newWord); 
       lexiconElement.addContent(newTag); 

       root.addContent(lexiconElement); 
       XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat()); 
       xmlOutput.output(readDoc, new FileOutputStream(new File("./src/Lexicon.xml"))); 


      } 

     } else { 

      Document doc = new Document(); // create a JDOM document 
      String freq = "1"; 
      Element theRoot = new Element("Lexicon"); // Creates a element named Lexicon and makes it the root 
      doc.setRootElement(theRoot); 

      Element lexiconElement = new Element("lexiconElement"); 
      Element Word = new Element("word"); 
      Element Tag = new Element("tag"); 
      Element frequency = new Element("frequency"); 

      Word.addContent(new Text(word)); 
      Tag.addContent(new Text(tag)); 
      frequency.addContent(new Text(freq)); 

      Tag.addContent(frequency); 
      lexiconElement.addContent(Word); 
      lexiconElement.addContent(Tag); 

      theRoot.addContent(lexiconElement); 
      XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat()); 
      xmlOutput.output(doc, new FileOutputStream(new File("./src/Lexicon.xml"))); 



     } 


    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 

Мне нужно получить значение в теге частотности и увеличить значение на единицу и добавить в тот же XML-файл. но он не работал с вышеуказанным кодом.

следующие несколько элементов avaialble в моем XML-файле.

<lexiconElement> 
    <word>හයිටිය</word> 
    <tag> 
     NNPI 
     <frequency>1</frequency> 
    </tag> 
    </lexiconElement> 
    <lexiconElement> 
    <word>-2</word> 
    <tag> 
     QFNUM 
     <frequancy>1</frequancy> 
    </tag> 
    </lexiconElement> 
    <lexiconElement> 
    <word>තමා</word> 
    <tag> 
     PRP 
     <frequancy>1</frequancy> 
    </tag> 
    </lexiconElement> 
+0

Предоставлять свой XML, а также –

ответ

1

Это относительно распространенная проблема со многими приложениями, а не только с JDOM.

При создании FileOutputStream, и писать на нем, вам надо выгружать и закройте IT перед выходом из программы.

Изменить:

xmlOutput.output(doc, new FileOutputStream(new File("./src/Lexicon.xml"))); 

быть (с использованием примерочных с-ресурсов):

try (OutputStream fileout = new FileOutputStream(new File("./src/Lexicon.xml"))) { 
    xmlOutput.output(doc, fileout); 
} 

или:

OutputStream fileout = new FileOutputStream(new File("./src/Lexicon.xml")); 
xmlOutput.output(doc, fileout); 
fileout.flush(); 
fileout.close(); 
Смежные вопросы