【发布时间】:2012-07-31 15:33:40
【问题描述】:
我想知道是否有人可以帮助将数据附加到 XML 文件中。下面,我有代码,它从另一个 xml 文件中获取数据,进行一些逻辑更改,并将更改写入新的输出文件。然而,通过这个api
Im having trouble understanding which method does appending. It seems like all of them creates a new XML file doc/overwrites an existing one if it exists. Im 在循环时尝试将标签附加到新文件。
for(int i = 0; i < itemList.size(); i++){
//get the specific item node
Element item = (Element)itemList.get(i);
//there are some non item nodes so need this check
if(item.getName().equals("item")){
//do some logic changing to the tags
//System.out.println(item.getValue());
//System.out.println(item.getChild("Q").getValue());
//System.out.println(item.getChild("A").getValue());
boolean exists = (new File("/Users/davidyu/Desktop/file2.xml")).exists();
//if file exists
if(exists){
System.out.println("in here1");
xmlOutput.?????
}
else{
System.out.println("in here2");
xmlOutput.output(doc, new FileWriter("/Users/davidyu/Desktop/file2.xml"));
}
}
我基本上想要做的是在每次循环迭代后将一个新的项目标签写入文件中。该项目标签应包含新的子节点“Q”和“A”。
我该怎么做?
【问题讨论】: