Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some more informations (code, expected XML etc.) would be helpful ...</p> <p>But here's an example how you can do this:</p> <p>The key feature is implementing a <a href="http://simple.sourceforge.net/download/stream/doc/javadoc/org/simpleframework/xml/convert/Converter.html" rel="nofollow noreferrer"><code>Converter</code></a>, where you can customize how objects are serialized / deserialized. In the following code I implement a <code>Converter</code> for the <code>Child</code> class, however it's possible to implement it for the <code>Parent</code> class instead too.</p> <h3>The <code>Child</code> class:</h3> <pre><code>@Root(name = "child") @Convert(value = ChildConverter.class) // Set the Converter public class Child { private boolean actived; @Element(name = "value", required = true) private String value; @Element(name = "value2", required = true) private int secondValue; public Child(boolean actived, String value, int secondValue) { this.actived = actived; this.value = value; this.secondValue = secondValue; } public boolean isActived() { return actived; } public String getValue() { return value; } public int getSecondValue() { return secondValue; } // ... } </code></pre> <p>Beside the <code>activated</code> flag this class has two other members to show how do serialize them.</p> <h3>The <code>Parent</code> class:</h3> <pre><code>@Root(name = "parent") public class Parent { @ElementList(name = "childs", required = true) private List&lt;Child&gt; childs; public Parent() { this.childs = new ArrayList&lt;&gt;(); } public void addChild(Child child) { childs.add(child); } // ... } </code></pre> <h3>The <code>Converter</code> implementation:</h3> <pre><code>public class ChildConverter implements Converter&lt;Child&gt; { @Override public Child read(InputNode node) throws Exception { throw new UnsupportedOperationException("Not supported yet."); } @Override public void write(OutputNode node, Child value) throws Exception { if( value.isActived() == true ) // Check if 'activated' flag is set { // Set valus of the child node.setValue(value.getValue()); node.setAttribute("secondValue", String.valueOf(value.getSecondValue())); } else { node.remove(); // Remove the node since we don't need it } } } </code></pre> <p>The implementation is not very complex so far. First we check if <code>activated</code> is set. If yes we fill the objects value into the node, if it's not set we remove the node (else you'll get a <code>&lt;child /&gt;</code> in your XML).</p> <h3>How to use:</h3> <pre><code>// Some test data Parent p = new Parent(); p.addChild(new Child(true, "a", 1)); p.addChild(new Child(true, "b", 2)); p.addChild(new Child(false, "c", 3)); // "disabled" p.addChild(new Child(true, "d", 4)); p.addChild(new Child(false, "e", 5)); // "disabled" p.addChild(new Child(false, "f", 6)); // "disabled" final File f = new File("test.xml"); Serializer ser = new Persister(new AnnotationStrategy()); // Don't forget 'AnnotationStrategy'! ser.write(p, f); // Serialize to a file or whatever you need </code></pre> <p>And finally ...</p> <h3>The XML output:</h3> <pre class="lang-xml prettyprint-override"><code>&lt;parent&gt; &lt;childs class="java.util.ArrayList"&gt; &lt;child secondValue="1"&gt;a&lt;/child&gt; &lt;child secondValue="2"&gt;b&lt;/child&gt; &lt;child secondValue="4"&gt;d&lt;/child&gt; &lt;/childs&gt; &lt;/parent&gt; </code></pre> <p>There are only <code>child</code> elements for object where <code>activated</code> was <code>true</code>, those with <code>false</code> are skipped.</p> <p><strong><em>Note:</strong> If you want to remove <code>class="java.util.ArrayList"</code> please see here: <a href="https://stackoverflow.com/questions/12113025/remove-class-attribute/12145092#12145092">Remove class= attribute</a></em></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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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