简体   繁体   English

使用JDOM2构建XML并添加数据

[英]build an XML with JDOM2 and add data

I'm new to using JDOM2 with java and i don't find how to not repeat the open tag of xml 我是将JDOM2与Java结合使用的新手,但我找不到如何不重复xml的open标签的方法

this look like this in the xml file when i created my "compte" : 当我创建“ 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>

this is the Java : 这是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));

thanks for your time :) 谢谢你的时间 :)

I've find the solution : 我找到了解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM