日期:2014-05-20 浏览次数:21034 次
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element adElement = doc.createElement("AD");
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
for (int i = 0; i < list.size(); i++) {
Element element = doc.createElement(list.get(i));
adElement.appendChild(element );
}
...保存...
public InputStream getStreamFromModel(ICommonModel model) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(baos, ENCODING);
Document doc = getModelFeature(model);
try {
OutputFormat format = new OutputFormat(doc, ResourcesPlugin.getEncoding(), true);
XMLSerializer serializer = new XMLSerializer(baos, format);
serializer.serialize(doc);
} catch (IOException e) {
e.printStackTrace();
} finally {
osw.close();
}
return new ByteArrayInputStream(baos.toByteArray());
}