2010-04-16 17 views
10

Agregué la solución al código a continuación.Agregue xml-stylesheet y obtenga standalone = yes

El código en la parte inferior es lo que tengo. Eliminé la creación de todas las etiquetas.

En la parte superior del archivo xml que obtengo. <?xml version="1.0" encoding="UTF-8" standalone="no"?> Tenga en cuenta que independiente es no, incluso si lo tengo configurado en sí.

La primera pregunta: ¿Cómo obtengo standalone = yes?

Me gustaría agregar <?xml-stylesheet type="text/xsl" href="my.stylesheet.xsl"?> en la línea dos en el archivo xml.

Segunda pregunta: ¿Cómo hago eso?

Algunos enlaces útiles? ¿Cualquier cosa?

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

Respuesta

10

I añadió

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

antes del corte y

doc.insertBefore(pi, root); 

justo después del elemento raíz se añade al doc.

3

en mi código, escribió:


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

de salida:

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

Cuestiones relacionadas