Robert Masaioli's answer gives an idea of โโusing Simple.
It is important to remember that Simple XML must be able to follow any structure that you can logically generate using classes. That way, you can simply create a BaseClass that uses the error interface and apply the Decorator pattern so that it passes all of this through a specific error class without any implementing objects that needed to know what they were given.
That probably doesn't make sense. How about what I just show you ... okay ... I just left and implemented exactly what I was thinking about, and here are the results (full link to the code):
Main file:
package com.massaiolir.simple.iface;
import java.io.File;
import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister;
public class Main {public static void main (String [] args) throws Exception {Serializer serial = new Persister (); ResC resc = serial.read (ResC.class, new file ("data / testdata.xml"));
System.out.println(" == Printing out all of the error text. == "); System.out.println(resc.getErrorText()); System.out.println(resc.conRes.getErrorText()); System.out.println(resc.conRes.conList.getErrorText()); for (Con con : resc.conRes.conList.cons) { System.out.println(con.getErrorText()); } System.out.println(" == Finished printing out all of the error text. == "); }
} It is simple and displays the results.
BaseObject.java class:
package com.massaiolir.simple.iface;
import org.simpleframework.xml.Element;
The public class BaseObject implements Error {@Element (name = "Err", required = false, type = ConcreteError.class) private Error err;
@Override public String getErrorText() { return err.getErrorText(); } @Override public void setErrorText(String errorText) { err.setErrorText(errorText); }
} This is a class that must expand if it wants to "Err".
Error Interface:
package com.massaiolir.simple.iface;
open interface Error {void setErrorText (String errorText);
String getErrorText();
} ConcreteError class:
package com.massaiolir.simple.iface;
import org.simpleframework.xml.Attribute;
Public class ConcreteError implements Error {@ Attribute private string text;
@Override public String getErrorText() { return text; } @Override public void setErrorText(String errorText) { this.text = errorText; }
} Actual implementation classes after this point. You will see that they are pretty trivial because the real work is handled in the classes above.
Con class:
package com.massaiolir.simple.iface;
Public class Con extends BaseObject {
} ConList class:
package com.massaiolir.simple.iface;
import java.util.ArrayList;
import org.simpleframework.xml.ElementList;
The public ConList class extends BaseObject {@ElementList (entry = "Con", inline = true) public ArrayList cons; } ConRes class:
package com.massaiolir.simple.iface;
import org.simpleframework.xml.Element;
The public class ConRes extends BaseObject {@Element (name = "ConList") public ConList conList; } ResC class:
package com.massaiolir.simple.iface;
import org.simpleframework.xml.Element; import org.simpleframework.xml.Root;
@root The public ResC class extends BaseObject {@Element (name = "ConRes") public ConRes conRes; } And thatโs all there is. Pretty simple. I was able to break it all in ten minutes. In fact, it took me longer to write this answer than it took me to write the code that I give you. If you do not understand anything about the code I just wrote, please let me know. Hope this helps you understand how you can do something like this.