Ho aggiunto la soluzione al codice seguente.Aggiungi xml-stylesheet e ottieni standalone = yes
Il codice in fondo è quello che ho. Ho rimosso la creazione di tutti i tag.
Nella parte superiore del file xml ottengo. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
Nota che standalone è no, anche tu lo ho impostato su yes.
La prima domanda: come ottengo standalone = sì?
Vorrei aggiungere <?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?>
alla riga due nel file xml.
Seconda domanda: come faccio?
Alcuni collegamenti utili? Nulla?
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")));