XML文件没有使用jdom进行更新 [英] XML file is not updating using the jdom

查看:109
本文介绍了XML文件没有使用jdom进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的java代码,用于读取xml文件并更新其中的一些值。

following is my java code for reading a xml file and updating some values in it.

 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);
    }
}

我需要获取frequancy标记和增量中的值将值加1并添加到同一个xml文件中。但它没有使用上面的代码。

i need to get the value in frequancy tag and increment the value by one and add to same xml file. but it didnt work with the above code.

以下是我的xml文件中可用的几个元素。

following are few elements avaialble in my xml file.

  <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>


推荐答案

这是许多应用程序的一个相对常见的问题,而不是只是JDOM。

This is a relatively common problem with many applications, not just JDOM.

当你创建一个FileOutputStream并写入它时,你 必须冲它并关闭它 退出程序之前。

When you create a FileOutputStream, and write to it, you HAVE TO FLUSH IT AND CLOSE IT before exiting your program.

更改:


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


(使用try-with-resources):

to be (using try-with-resources):

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();

这篇关于XML文件没有使用jdom进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆