Ciao a tutti.
Ecco il mio quesito: dato il seguente xml, come faccio a formattarlo in maniera tale da ottenere una corretta indentazione?
Premetto che ho tentato la seguente soluzione, ma senza alcun risultato:codice:<?xml version="1.0" encoding="UTF-8" standalone="no"?> <LIBRERIA> <LIBRO id="1"> <AUTORE>prova</AUTORE> <TITOLO>prova</TITOLO> <ANNO>2012</ANNO> </LIBRO> <LIBRO> <AUTORE>gio</AUTORE> <TITOLO>gio</TITOLO> <ANNO>2</ANNO> </LIBRO> <LIBRO> <AUTORE>p</AUTORE> <TITOLO>p</TITOLO> <ANNO>loi</ANNO> </LIBRO> <LIBRO> <AUTORE>9</AUTORE> <TITOLO>00</TITOLO> <ANNO>yy</ANNO> </LIBRO> <LIBRO> <AUTORE>kk</AUTORE> <TITOLO>jhfjgv</TITOLO> <ANNO>867</ANNO> </LIBRO> <LIBRO> <AUTORE>oo</AUTORE> <TITOLO>kk</TITOLO> <ANNO>9898</ANNO> </LIBRO> <LIBRO> <AUTORE>OTTO</AUTORE> <TITOLO>OTTO</TITOLO> <ANNO>1234</ANNO> </LIBRO> <LIBRO> <AUTORE>cxvcx</AUTORE> <TITOLO>kjghkg</TITOLO> <ANNO>9</ANNO> </LIBRO> </LIBRERIA>
Grazie a tutti x l'aiuto...codice:DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("libri.xml")); TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", new Integer(2)); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(document); //Stampol'output sul file xml StreamResult result1 = new StreamResult(new File("libri.xml")); transformer.transform(source, result1); //Stampo l'output sulla console StreamResult result2 = new StreamResult(System.out); transformer.transform(source, result2);

Rispondi quotando

