简体   繁体   中英

Writing XML file from an ArrayList - wrong output

I'm trying to write a XML file from an ArrayList<Element> . However, the output is not what I expected and I can't seem to find out the source of the problem. Can you please help me?

Here is some of my code:

    if (e.getSource() == saveMenu) {
        writeToXML();
        JOptionPane.showMessageDialog(Simulation.this, 
                    "File saved", 
                    "Simulation", JOptionPane.INFORMATION_MESSAGE);

    }

 public void writeToXML() throws IOException {

     int size = alElementToXML.size();
     for (int i = 0; i < size; i++) {
         alIds.add(alElementToXML.get(i).attributeValue("id"));
     }

     OutputFormat format = OutputFormat.createPrettyPrint();
     format.setEncoding("UTF-8");
     XMLWriter xmlWriter = null;
     xmlWriter = new XMLWriter(new OutputStreamWriter(
             new FileOutputStream("Simulation_" + MASG_GUI.getContainerNameTxt().getText() + "_Details.xml"), "UTF8"),
             format);
     try {
         for (int i = 0; i < size; i++) {
             xmlWriter.write(configs.XMLwriterDOM4J.createXMLDocumentForSimulations(alElementToXML, alIds.get(i)));
         }
     } finally {

         xmlWriter.flush();
         xmlWriter.close();
     }
 }

This is the createXMLDocumentForSimulations method:

public static Document createXMLDocumentForSimulations(ArrayList<Element> elems, String id) {

    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("simulation");
    Element agent1 = root.addElement("member").addAttribute("id", id);
    int size = Simulation.getAlElementToXML().size();
    for (int i = 0; i < size; i++) {           
         agent1.addElement(elems.get(i).asXML());
    }
    return document;
}

and one example of the output I get:

<?xml version="1.0" encoding="UTF-8"?>

<simulation>
  <member id="House">
    <<member id="House"><id>1</id><type>1</type><max_usage>1</max_usage><min_usage>1</min_usage><average_usage>1</average_usage></member>/>
    <<member id="CSP"><id>1</id><type>1</type></member>/>
    <<member id="VPP"><id>6</id><type>6</type></member>/>
  </member>
</simulation>
<?xml version="1.0" encoding="UTF-8"?>

<simulation>
  <member id="CSP">
    <<member id="House"><id>1</id><type>1</type><max_usage>1</max_usage><min_usage>1</min_usage><average_usage>1</average_usage></member>/>
    <<member id="CSP"><id>1</id><type>1</type></member>/>
    <<member id="VPP"><id>6</id><type>6</type></member>/>
  </member>
</simulation>

What I wanted was something like this:

<?xml version="1.0" encoding="UTF-8"?>

<simulation>
  <member id="House">
    <id>1</id>
    <type>1</type>
    <max_usage>1</max_usage>
    <min_usage>1</min_usage>
    <average_usage>1</average_usage>
  </member>
  <member id="VPP">
    <id>6</id>
    <type>6</type>
  </member>
</simulation>

What am I doind wrong and how can I fix it to obtain the expected output?

Thanks

首先,你的xmlWriter周围有一个for循环,这就是你有两个<simulation>根元素的原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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