Подтвердить что ты не робот

Добавить xml-stylesheet и получить автономный = да

Я добавил решение в код ниже.

Код внизу - это то, что у меня есть. Я удалил создание всех тегов.

В верхней части в xml файле я получаю. <?xml version="1.0" encoding="UTF-8" standalone="no"?> Обратите внимание, что автономный нет, даже если у меня он установлен на yes.

Первый вопрос: как получить автономный = да?

Я хотел бы добавить <?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")));
4b9b3361

Ответ 1

Я добавил

doc.setXmlStandalone(true);
ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"my.stylesheet.xsl\"");`  

перед разрезом и

doc.insertBefore(pi, root);

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

Ответ 2

в моем коде, я написал:


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlStandalone(true);

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");

выход:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>