Как создать разрывы строк в XML с помощью Java

Я хочу создать XML-файл с разрывами строк, который выглядит примерно так:

<Instrument currencyId="THB"
disabledCount="0"
hasBeenEnabledOnce="TRUE"
</Instrument>

Используя этот код, я приблизился, но все атрибуты оказались в одной строке:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();

Element rootElement = doc.createElement("Instrument");
doc.appendChild(rootElement);

Instrument.setAttribute("currencyId", "THB");
Instrument.setAttribute("disabledCount", "0");
Instrument.setAttribute("hasBeenEnabledOnce", "TRUE");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\file.xml"));
    transformer.transform(source, result);

Я получаю следующий результат:

<Instrument currencyId="THB" disabledCount="0" hasBeenEnabledOnce="TRUE"

Как я могу добавить разрывы строк между атрибутами?

просто небольшое замечание, я подумал, что transformer.setOutputProperies мог бы решить эту проблему, выполнив поиск похожих проблем, которые я получил с помощью "OutputKeys.INDENT", "yes" и indent-amount -> "4". Но ничего не изменилось. Не имеет значения, открываю ли я файл в notepad++ или как веб-страницу.

1 ответ

Я бы посоветовал вам один раз сослаться на эту ссылку во время работы над XML и переносами строк.

Другие вопросы по тегам