【问题标题】:build an XML with JDOM2 and add data使用 JDOM2 构建 XML 并添加数据
【发布时间】:2014-01-08 16:33:05
【问题描述】:

我是在 java 中使用 JDOM2 的新手,我不知道如何不重复 xml 的打开标记

当我创建“compte”时,xml 文件中的内容如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<banque>
  <compte>
    <numCompte>4465</numCompte>
    <nom>Antoine</nom>
    <solde>1684185</solde>
  </compte>
</banque><?xml version="1.0" encoding="UTF-8"?>
<banque>
  <compte id="0102">
    <numCompte>0102</numCompte>
    <nom>rzrzr</nom>
    <solde>85416</solde>
  </compte>
</banque>

这是Java:

Element banque = new Element("banque");  

               Document document = new Document(banque);  

               Element compte = new Element("compte");  

               compte.setAttribute(new Attribute("id", this.idCompte));
               compte.addContent(new Element("numCompte").setText(this.idCompte));  
               compte.addContent(new Element("nom").setText(this.nom));  
               compte.addContent(new Element("solde").setText(this.solde));  

               document.getRootElement().addContent(compte);  

               XMLOutputter xmlOutput = new XMLOutputter();  

               xmlOutput.output(document, System.out);  

               xmlOutput.setFormat(Format.getPrettyFormat());  
               xmlOutput.output(document, new FileWriter(  
                 "generatedXmlFiles/listeCompte.xml",true));

感谢您的宝贵时间:)

【问题讨论】:

  • 您可以在banque中添加compte内容并添加banque,最后添加到文档中。

标签: java xml jdom-2


【解决方案1】:

我找到了解决方案:

 try{

          Document document = null;
          Element root = null;
          File xmlFile = new File("generatedXmlFiles/listeCompte.xml");

        if(xmlFile.exists()){

              FileInputStream fis = new FileInputStream(xmlFile);
              SAXBuilder sb = new SAXBuilder();
              document = sb.build(fis);
              root = document.getRootElement();
              fis.close();
          }else{
              document = new Document();
              root = new Element("banque");
          }

          Element compte = new Element("compte");
          compte.setAttribute(new Attribute("idCompte", this.idCompte));
          compte.addContent(new Element("numCompte").setText(this.idCompte));
          compte.addContent(new Element("nom").setText(this.nom));
          compte.addContent(new Element("solde").setText(this.solde));

          root.addContent(compte);
          document.setContent(root);

          FileWriter writer = new FileWriter("generatedXmlFiles/listeCompte.xml");
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            outputter.output(document, writer);
            outputter.output(document, System.out);
            writer.close(); // close writer

          } catch (IOException io) {
            System.out.println(io.getMessage());
          } catch (JDOMException e) {
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    相关资源
    最近更新 更多