我正在使用下面的代码。但是,当我运行代码时,我想将新的 XML 附加到另一个下方的同一个 TXT 文件中。使用 JDOM 是否可行。请帮助我..
xmlOutput.output(doc, new FileWriter("c:\updated.txt")); 是需要修改的吗?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Replacer {
public static void main(String[] args) {
try {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("c:\\file.xml");
String temp = "1234567";
Document doc = (Document) builder.build(xmlFile);
Element rootNode = doc.getRootElement();
System.out.println("Root Node is --> "+rootNode);
Element ShipmentLines = rootNode.getChild("ShipmentLines");
Element ShipmentLine = ShipmentLines.getChild("ShipmentLine");
Element Containers = rootNode.getChild("Containers");
Element Container = Containers.getChild("Container");
ShipmentLine.getAttribute("OrderNo").setValue(temp);
Container.getAttribute("ContainerScm").setValue(temp);
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("c:\\updated.txt"));
} catch (IOException io) {
io.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
}
}