Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't explicitly require a 3rd party library to generate your rather simple XML format. You could simply write a to a StringBuffer object:</p> <pre><code>... StringBuffer sb = new StringBuffer(); sb.append("&lt;root&gt;"); for (String[] row: dataList) { sb.append("&lt;entry&gt;"); sb.append("&lt;ContractDate&gt;").append(row[0]).append("&lt;/ContractDate&gt;"); // etc... sb.append("&lt;/entry&gt;"); } sb.append("&lt;/root&gt;"); ... </code></pre> <p>However, I'm not necessarily advocating it. I personally recommend <a href="http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php" rel="nofollow">Simple XML</a>. Create a POJO to model your rows (I won't include all properties in my example):</p> <pre><code>@Root public class Entry { @Element private String trader; @Attribute private int id; public Entry() { super(); } public String getTrader() { return text; } public void setTrader(String trader) { this.trader = trader; } public int getId() { return id; } public void setId(int id) { this.id = id; } } @Root(name="root") public class Entries { @ElementList(inline=true) List&lt;Entry&gt; entries; public Entries(List&lt;Entry&gt; entries) { this.entries = entries; } } </code></pre> <p>It's certainly some extra work up-front, but for non-trivial programs, having proper Java classes to represent the data models makes a lot of sense and provides greater flexibility, and code clarity.</p> <p>To save save as XML you do this:</p> <pre><code> List&lt;Entry&gt; entryList = new ArrayList(); int entryCount = 0; Serializer serializer = new Persister(); for (String[] row: dataList) { Entry entry = new Entry(); entry.setTrader(row[1]); entry.setId(++entryCount); entryList.add(entry); } File result = new File("entries.xml"); serializer.write(new Entries(entryList), result); </code></pre> <p>The Simple XML library gives you a lot of flexibility. You can also load up the XML file back into a list of Entry objects.</p>
 

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