Вещь крайне простая, но поймал себя, что просто не получалось.
В результате родилось следующее
Код:
import bitel.billing.server.util.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import bitel.billing.server.contract.bean.ContractManager;
public void main( setup, con, conSlave )
{
//получаем 50 первых договоров и формируем из них xml-строки
cm = new ContractManager(con);
accounts = new StringBuilder();
for (cid=1;cid<50;cid++){
try{ accounts.append("<account>"+cm.getContractById(cid).getTitle()+"</account>");}
catch (NullPointerException e) {}
}
Source xmlInput = new StreamSource(new StringReader("<account_list><date>"+ new Date()+"</date>"+accounts+"</account_list>"));
StreamResult xmlOutput = new StreamResult(new FileWriter("test.xml"));
//Настраиваем трансформер
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "windows-1251");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
//flush&close
xmlOutput.getWriter().close();
}
Однако, всё ещё кажется громоздким. Может кто поделится более красивым решением?