ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 使用jdom向现有xml添加内容

2019-05-29 16:50:16  阅读:225  来源: 互联网

标签:java xml document class jdom


package xml.dierenshop.flaming.v1;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.jdom2.output.Format;
import java.io.FileWriter;
import java.io.IOException;

public class Writer {

    public void Writer(String categorie, String code, String naamartikel, String beschrijvingartikel, double prijz, String imgurl, String imgurl2, String imgurl3, String imgurl4, String imgurl5) {
        String prijs = String.valueOf(prijz);
        Document document = new Document();
        Element root = new Element("productlist");
        String naamelement = "naam";
        String categorieelement = "category";
        String descriptionelement = "description";
        Element child = new Element("product");
        child.addContent(new Element(categorieelement).setText(categorie));
        child.addContent(new Element("code").setText(code));
        child.addContent(new Element(naamelement).setText(naamartikel));
        child.addContent(new Element(descriptionelement).setText(beschrijvingartikel));
        child.addContent(new Element("price").setText(prijs));
        child.addContent(new Element("image").setText(imgurl));
        child.addContent(new Element("image").setText(imgurl2));
        child.addContent(new Element("image").setText(imgurl3));
        child.addContent(new Element("image").setText(imgurl4));
        child.addContent(new Element("image").setText(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);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我用我的主类编写带有变量的xml文件的类.
这里的输出是:

http://pastebin.com/nFtiv2b8

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

解决方法:

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

创建一个产品类来保存产品数据.将每个产品数据作为参数传递给方法是不行的.

产品类(为简单起见,所有字段都是公开的,这不是一个好的做法,你应该至少保护它们,并为每个字段设置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();
    }
}

最后一点点测试

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

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

标签:java,xml,document,class,jdom
来源: https://codeday.me/bug/20190529/1179345.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有