2010-01-28 9 views

risposta

4

È possibile utilizzare uno StringWriter di scrivere prima le informazioni foglio di stile in esso, e quindi il marshalling l'oggetto in esso:

StringWriter writer = new StringWriter(); 
//add processing instructions "by hand" with escaped quotation marks 
//or single marks 
writer.println("<?xml version='1.0'?>"); 
writer.println("<?xml-stylesheet type=\"text/xsl\" href=\"\">"); 

//create and configure marshaller to leave out processing instructions 
Marshaller marshaller = context.createMarshaller(); 
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); 

//marshal to the StringWriter 
marshaller.marshal(someObject,writer); 
//get the string representation 
String str = writer.toString(); 

Naturalmente è anche possibile stampare direttamente su ogni altro flusso di output che si desidera, ad esempio, file o Sytstem.out.

2

vedere come è fatto in rexsl-core, parte di ReXSL quadro XSL/JAXB/JAX-RS: XslResolver:

final String header = String.format(
    "\n<?xml-stylesheet type='text/xsl' href='%s'?>", 
    StringEscapeUtils.escapeXml("my-stylesheet.xsl") 
); 
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", header); 
1
JAXBContext jaxbContext; 
try { 
     jaxbContext = JAXBContext.newInstance(new Class[] {SomeObject.class}); 

     StringWriter writer = new StringWriter(); 
     writer.write("<?xml version='1.0'?>"); 
     writer.write("\n"); 
     writer.write("<?xml-stylesheet type=\"text/xsl\" href=\"\">"); 
     writer.write("\n"); 

     Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
     jaxbMarshaller.marshal(someobject, writer); 
} 
+4

Si prega di provare a rispondere a parole e non solo nel codice. E dal momento che rispondi così tardi, in che modo la tua risposta è diversa dalle altre? –

+0

Sembra una soluzione. –

9

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); 
+0

Questo è il modo elegante per farlo, ma non funzionerà se si sta eseguendo 'marshal()' in un 'File' (per esempio). Richiede che l'output sia Writer o Stream. Una volta a conoscenza di ciò, dovrebbe essere semplice racchiudere il 'File' in un' FileWriter'. –

+0

@KevinSadler Per favore, sentiti libero di aggiornare la mia risposta! –

+0

@KevinSadler La tua modifica è stata rifiutata dalla comunità. Ho aggiunto la modifica, ma è stata uglyfied. Per favore, lo stimoli in modo tale che la modifica sia collegata a te! Grazie! –

Problemi correlati