JAXB also comes βfreeβ with Java 6. If you have control over the XML format (vs must accept an external schema), then JAXB is trivial to use with a little more than a few annotations and very simple sorting code.
A simple toXML method:
JAXBContext ctx = JAXBContext.newInstance(YourClass.class); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter sw = new StringWriter(); m.marshal(waypointServer, sw); sw.close(); return sw.toString();
XML Reader:
URL url = new URL(filePath); JAXBContext ctx = JAXBContext.newInstance(YourClass.class); Unmarshaller um = ctx.createUnmarshaller(); YourClass yc = (YourClass)um.unmarshal(url.openStream());
Simple bean:
@XmlRootElement public class YourClass { List<Stuff> stuffList; String id; int cnt;
It can get more complicated, obviously, but out of the box it can be very simple.
source share