Я добавил решение кода ниже.
Код внизу - то, что я имею. Я удалил создание всех тегов.
Наверху в XML-файле я добираюсь.<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Обратите внимание, что автономный не, даже Вы у меня есть он набор к да.
Первый вопрос: Как я становлюсь автономным = да?
Я хотел бы добавить <?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?>
в строке два в XML-файле.
Второй вопрос: Как я делаю это?
Некоторые полезные ссылки? Что-нибудь?
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.setXmlStandalone(true);
ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\"");
Element root = doc.createElement("root-element");
doc.appendChild(root);
doc.insertBefore(pi, root);
<cut>
TransformerFactory transfac = TransformerFactory.newInstance();
transfac.setAttribute("indent-number", new Integer(2));
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "name");
FileOutputStream fout = new FileOutputStream(filepath);
BufferedOutputStream bout= new BufferedOutputStream(fout);
trans.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(bout, "utf-8")));
Я добавил
doc.setXmlStandalone(true);
ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\"");`
перед вырезом и
doc.insertBefore(pi, root);
сразу после того, как корневой элемент был добавлен в документ.