gpt4 book ai didi

java - XML 文件未使用 jdom 更新

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:29 24 4
gpt4 key购买 nike

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

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

我需要获取频率标签中的值并将该值递增 1,然后添加到同一个 xml 文件中。但它不适用于上述代码。

以下是我的 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>

最佳答案

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

当您创建 FileOutputStream 并向其写入数据时,您必须在退出程序之前刷新并关闭它

改变:

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

成为(使用 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();

关于java - XML 文件未使用 jdom 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26108513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com