XmlHandler - TahliaK/wight-whale GitHub Wiki
XmlHandler provides a simple method for reading and writing in XML any appropriately annotated Java class.
Appropriate annotation looks something like this:
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Foo {
@XmlElement (name = "someName")
private int field;
}
There are 3 methods/tasks:
- Create the XmlHandler for your class
XmlHandler<Bar> handler = new XmlHandler<Bar>(new Bar().getClass());
- Use it to read from your file (in the "Files/" directory)
Bar barObj = handler.readFromXml("directory", "filename.extension")
- Use it to write to a file (in the "Generated/" directory)
handler.writeToXml(barObj, "directory", "Bar_ID");
Notes:
- The constructor can fail. If it does, it will use Log.send(ERROR...)
- You can check in-program if the xmlHandler is functioning with the
status()
function - If the constructor failed, nothing will execute and all returns will be null
- Ouputs go to "[Project file]/Generated/[directory]/[itemId].xml".
- Inputs are searched for in "[Project file]/Files/[directory]/[filename]" (Note that ".xml" extension is NOT included in inputs)
- [directory] variables are optional; to use the root in/out folder, enter
""
for directory.