Tutte le soluzioni qui sono abbastanza brutto e prolisso. Basta impostare la linea all'interno dell'oggetto Mashaller
specificando l'intestazione aggiuntiva.
Marshaller jaxbMarshaller = ...
jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders",
"<?xml-stylesheet type='text/xsl' href='nameoffile.xsl' ?>");
Questo esempio genera un oggetto XML in un file utilizzando un foglio di stile e formatta gli elementi in modo che possano essere letti dagli utenti. L'oggetto myXmlObject
è di classe MyXmlClass
, e sarà scritto file
, formattata da un foglio di stile data dal xslUrl
:
JAXBContext context = JAXBContext.newInstance(MyXmlClass.class);
Marshaller marshaller = context.createMarshaller();
//Need to use a Writer to marshal with the XSL
FileWriter fw = new FileWriter(file);
//Do this or else the XML is all one line and not human friendly...
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders",
"<?xml-stylesheet type='text/xsl' href=\"" +
xslUrl +
"\" ?>");
marshaller.marshal(myXmlObject, fw);
fonte
2013-09-26 16:09:38
Vuoi dire che si desidera includere un riferimento foglio di stile in uscita JAXB? – skaffman