Ich habe die Lösung dem folgenden Code hinzugefügt.Fügen Sie xml-stylesheet hinzu und erhalten Sie standalone = yes
Der Code an der Unterseite ist, was ich habe. Ich habe die Erstellung aller Tags entfernt.
Oben in der XML-Datei bekomme ich. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
Beachten Sie, dass Standalone nicht ist, auch wenn ich es auf ja eingestellt habe.
Die erste Frage: Wie bekomme ich standalone = yes?
Ich möchte <?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?>
Zeile zwei in der XML-Datei hinzufügen.
Zweite Frage: Wie mache ich das?
Einige nützliche Links? Etwas?
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")));