我的xml文件如下
<lexicon>
<lexiconElement>
<word>xxxx</word>
<tag>NN
<frequancy>3</frequancy>
</tag>
<tag>VB
<frequancy>2</frequancy>
</tag>
</lexiconElement>
</lexicon>
我需要知道在使用JDOM更新时如何在同一个xml文件中将多个元素附加到同一级别,以及如何使用JDOM读取同一级别中具有相同名称的元素
答案 0 :(得分:1)
我希望这就是你要找的东西。
try {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("D:\\your_file.xml");
Document doc = (Document) builder.build(xmlFile);
Element rootNode = doc.getRootElement();
Element lexiconElement = rootNode.getChild("lexiconElement");
// 1. add new <tag> elements to xml
Element newTag = new Element("your_new_tag").setText("your_new_tag_value");
lexiconElement.addContent(newTag);
// 2. Update the text between <frequancy> in perticular tag
//lexiconElement.getChild("tag").getChild("frequancy").setText("9");
// 2. Update the text between <frequancy> in all tag
List<Element> list = lexiconElement.getChildren("tag");
for(Element elm : list){
elm.getChild("frequancy").setText("324");
}
// 3. Update the text between <word>
lexiconElement.getChild("word").setText("yyyy");
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.output(doc, new FileWriter("D:\\your_file.xml"));
System.out.println("*********Done************");
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}