Java 使用jdom向现有xml添加内容

Java 使用jdom向现有xml添加内容,java,xml,class,document,jdom,Java,Xml,Class,Document,Jdom,这是我的类,用于使用主类中的变量编写xml文件。 这里的输出是: 现在我有一个问题,下次运行这个java应用程序时,我希望它添加一个新产品,但保留旧产品。但是每次我尝试这个方法时,它都会用新数据替换旧数据。基本上,您需要加载一个现有的xml文件,并通过解析它并从中获取根元素来创建一个文档。如果文件不存在,则创建一个新文档和一个新的根元素。之后,您可以继续使用显示的代码 创建一个类Product来保存产品数据。将产品数据作为参数传递给方法是不可能的 Product类(为了简单起见,所有字段都是公

这是我的类,用于使用主类中的变量编写xml文件。 这里的输出是:


现在我有一个问题,下次运行这个java应用程序时,我希望它添加一个新产品,但保留旧产品。但是每次我尝试这个方法时,它都会用新数据替换旧数据。

基本上,您需要加载一个现有的xml文件,并通过解析它并从中获取根元素来创建一个
文档。如果文件不存在,则创建一个新文档和一个新的根元素。之后,您可以继续使用显示的代码

创建一个类
Product
来保存产品数据。将产品数据作为参数传递给方法是不可能的

Product类(为了简单起见,所有字段都是公共的,这不是一个好的实践,您至少应该使它们受到保护,并且对于每个字段都有一个getter和setter方法)

书写方法

public class Product {
    public String categorie;
    public String code;
    public String naamartikel;
    public String beschrijvingartikel;
    public double prijz;
    public String imgurl;
    public String imgurl2;
    public String imgurl3; 
    public String imgurl4;
    public String imgurl5;
}
最后是一个小测试

public static void Writer(Product product) throws JDOMException, IOException {

    Document document = null;
    Element root = null;

    File xmlFile = new File("products.xml");
    if(xmlFile.exists()) {
        // try to load document from xml file if it exist
        // create a file input stream
        FileInputStream fis = new FileInputStream(xmlFile);
        // create a sax builder to parse the document
        SAXBuilder sb = new SAXBuilder();
        // parse the xml content provided by the file input stream and create a Document object
        document = sb.build(fis);
        // get the root element of the document
        root = document.getRootElement();
        fis.close();
    } else {
        // if it does not exist create a new document and new root
        document = new Document();
        root = new Element("productlist");
    }


    String prijs = String.valueOf(product.prijz);
    String naamelement = "naam";
    String categorieelement = "category";
    String descriptionelement = "description";
    Element child = new Element("product");
    child.addContent(new Element(categorieelement).setText(product.categorie));
    child.addContent(new Element("code").setText(product.code));
    child.addContent(new Element(naamelement).setText(product.naamartikel));
    child.addContent(new Element(descriptionelement).setText(product.beschrijvingartikel));
    child.addContent(new Element("price").setText(prijs));
    child.addContent(new Element("image").setText(product.imgurl));
    child.addContent(new Element("image").setText(product.imgurl2));
    child.addContent(new Element("image").setText(product.imgurl3));
    child.addContent(new Element("image").setText(product.imgurl4));
    child.addContent(new Element("image").setText(product.imgurl5));
    root.addContent(child);
    document.setContent(root);
    try {
        FileWriter writer = new FileWriter("products.xml");
        XMLOutputter outputter = new XMLOutputter();
        outputter.setFormat(Format.getPrettyFormat());
        outputter.output(document, writer);
        outputter.output(document, System.out);
        writer.close(); // close writer
    } catch (IOException e) {
        e.printStackTrace();
    }
}

另外,文件名
products.xml
不应该硬编码到java文件中;而是在运行程序时将其作为参数传递。

在将root设置为文档内容之前,请使用:

public static void main(String[] args) throws JDOMException, IOException {
    Product product = null;

    product = new Product();
    product.categorie = "cat1";
    product.code = "code1";
    product.naamartikel = "naam1";
    product.beschrijvingartikel = "beschrijving1";
    product.prijz = 100d;
    product.imgurl = "http://localhost/img1.png";
    product.imgurl2 = "http://localhost/img2.png";
    product.imgurl3 = "http://localhost/img3.png";
    product.imgurl4 = "http://localhost/img5.png";
    product.imgurl5 = "http://localhost/img5.png";
    Writer(product);

    product = new Product();
    product.categorie = "cat2";
    product.code = "code2";
    product.naamartikel = "naam2";
    product.beschrijvingartikel = "beschrijving2";
    product.prijz = 200d;
    product.imgurl = "http://localhost/img21.png";
    product.imgurl2 = "http://localhost/img22.png";
    product.imgurl3 = "http://localhost/img23.png";
    product.imgurl4 = "http://localhost/img25.png";
    product.imgurl5 = "http://localhost/img25.png";
    Writer(product);

    product = new Product();
    product.categorie = "cat3";
    product.code = "code3";
    product.naamartikel = "naam3";
    product.beschrijvingartikel = "beschrijving3";
    product.prijz = 300d;
    product.imgurl = "http://localhost/img31.png";
    product.imgurl2 = "http://localhost/img32.png";
    product.imgurl3 = "http://localhost/img33.png";
    product.imgurl4 = "http://localhost/img35.png";
    product.imgurl5 = "http://localhost/img35.png";
    Writer(product);
}

因为一个元素只能与jdom中的一个文档关联。

替换
root=document.getRootElement()

root = root.detach();

因为一个元素只能与jdom中的一个文档关联。

请在此处发布您的代码。我真诚的道歉,我真的不知道如何在stackoverflow上执行此操作。当我执行此操作时,问题表单上有一个名为“代码示例”
(Ctrl+K)
@zvzdhk的特殊按钮,然后粘贴我的代码真的很乱吗?去其他网站寻找你的代码更烦人我在学校里设法找到了一些类似的东西,不过非常感谢!
root = root.detach();
root = document.detachRootElement();