Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting XML file from an ArrayList - wrong output
    primarykey
    data
    text
    <p>I'm trying to write a <code>XML</code> file from an <code>ArrayList&lt;Element&gt;</code>. 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?</p> <p>Here is some of my code:</p> <pre><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 &lt; 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 &lt; size; i++) { xmlWriter.write(configs.XMLwriterDOM4J.createXMLDocumentForSimulations(alElementToXML, alIds.get(i))); } } finally { xmlWriter.flush(); xmlWriter.close(); } } </code></pre> <p>This is the <code>createXMLDocumentForSimulations</code> method:</p> <pre><code>public static Document createXMLDocumentForSimulations(ArrayList&lt;Element&gt; 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 &lt; size; i++) { agent1.addElement(elems.get(i).asXML()); } return document; } </code></pre> <p>and one example of the output I get:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;simulation&gt; &lt;member id="House"&gt; &lt;&lt;member id="House"&gt;&lt;id&gt;1&lt;/id&gt;&lt;type&gt;1&lt;/type&gt;&lt;max_usage&gt;1&lt;/max_usage&gt;&lt;min_usage&gt;1&lt;/min_usage&gt;&lt;average_usage&gt;1&lt;/average_usage&gt;&lt;/member&gt;/&gt; &lt;&lt;member id="CSP"&gt;&lt;id&gt;1&lt;/id&gt;&lt;type&gt;1&lt;/type&gt;&lt;/member&gt;/&gt; &lt;&lt;member id="VPP"&gt;&lt;id&gt;6&lt;/id&gt;&lt;type&gt;6&lt;/type&gt;&lt;/member&gt;/&gt; &lt;/member&gt; &lt;/simulation&gt; &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;simulation&gt; &lt;member id="CSP"&gt; &lt;&lt;member id="House"&gt;&lt;id&gt;1&lt;/id&gt;&lt;type&gt;1&lt;/type&gt;&lt;max_usage&gt;1&lt;/max_usage&gt;&lt;min_usage&gt;1&lt;/min_usage&gt;&lt;average_usage&gt;1&lt;/average_usage&gt;&lt;/member&gt;/&gt; &lt;&lt;member id="CSP"&gt;&lt;id&gt;1&lt;/id&gt;&lt;type&gt;1&lt;/type&gt;&lt;/member&gt;/&gt; &lt;&lt;member id="VPP"&gt;&lt;id&gt;6&lt;/id&gt;&lt;type&gt;6&lt;/type&gt;&lt;/member&gt;/&gt; &lt;/member&gt; &lt;/simulation&gt; </code></pre> <p>What I wanted was something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;simulation&gt; &lt;member id="House"&gt; &lt;id&gt;1&lt;/id&gt; &lt;type&gt;1&lt;/type&gt; &lt;max_usage&gt;1&lt;/max_usage&gt; &lt;min_usage&gt;1&lt;/min_usage&gt; &lt;average_usage&gt;1&lt;/average_usage&gt; &lt;/member&gt; &lt;member id="VPP"&gt; &lt;id&gt;6&lt;/id&gt; &lt;type&gt;6&lt;/type&gt; &lt;/member&gt; &lt;/simulation&gt; </code></pre> <p>What am I doind wrong and how can I fix it to obtain the expected output?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload