If you are ok with annotations, something like Simple XML Serialization can do something like what you are looking for. I also like XStream, but it may not give itself to an XML that is as different from the class structure as yours is.
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php
确实,你也许会更不用说写一部具有像这样强大缓冲的长官:
public Element marshal() {
StringBuffer sb = new StringBuffer();
sb.append(" <bean id="">
");
sb.append(" <property name="").append(name).append(""/>
");
sb.append(" </bean>
");
InputStream istream = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
Document myDoc = new SAXBuilder().build(istream);
}
或更好地形成文件并增加内容。
然后产出
// Save it to a file:
XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
java.io.FileWriter writer = new java.io.FileWriter(fileName);
out.output(document, writer);
writer.flush();
writer.close();
我认为,如果你的计划非常简单,但不同于你的XML,你可能会花更多的时间试图混淆XML的飞行员。