2010-04-16 23 views
10

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"))); 

risposta

10

ho aggiunto

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

prima del taglio e

doc.insertBefore(pi, root); 

subito dopo l'elemento principale è stato aggiunto al documento.

3

nel mio codice, ho scritto:


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"); 

uscita:

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

Problemi correlati