Salve a tutti, chiedevo se qualcuno poteva aiutarmi in un piccolo problema.
Allora, ho realizzato una servlet http che prende i dati da una form html (recupera il titolo e il testo di una relazione da una textarea) e li inserisce in UN XML GIA' ESISTENTE.
Cioè io ho creato il file "relazioni.xml" e al suo interno dovrebbe scrivere così:
La furbata è che la servlet legge l'attributo root e non sovrascrive il contenuto ma inserisce dentro i tag root appunto i nuovi tag per ogni nuova relazione!!Però tutto tutto non poteva andare liscio...codice:<?xml version="1.0" encoding="UTF-8"?> <root> <relazione> <autore>dave83</autore> <titolo>regergggggggggggggg</titolo> <testo>qui puoi scrivere al tua relazionefasddddddddddddddddddddd</testo> <revisore>nessun revisore</revisore> </relazione> </root>
Invece di scrivermi come sopra mi scrive così:
Adesso posto anche il codice così magari vi spiego anche un'altra cosa:codice:<?xml version="1.0" encoding="UTF-8"?> <root> </root> <?xml version="1.0" encoding="UTF-8"?> <root> <relazione> <autore>dave83</autore> <titolo>regergggggggggggggg</titolo> <testo>qui puoi scrivere al tua relazionefasddddddddddddddddddddd</testo> <revisore>nessun revisore</revisore> </relazione> </root>
Nelle 2 righe rosse ho evidenziato ciò che secondo me risolve il problema, però il compilatore non prende setIndent e setNewlines come boolean e non lo compila!!!!!!!!!!!!HELP vi prego!!!!!!!!!!!!!!!!!!!!!!!!!codice:import java.net.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.jdom.*; import org.jdom.input.*; import java.util.*; import org.jdom.output.*; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; public class word extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); ServletConfig c = getServletConfig(); ServletContext context = c.getServletContext(); PrintWriter out = response.getWriter(); HttpSession se=request.getSession(true); Date today = new Date(); String data = today.getDate()+"-"+(today.getMonth()+1)+"-"+(today.getYear()+1900)+"--"+ today.getHours()+"."+today.getMinutes()+"."+today.getSeconds()+""; String loguser; String rev="nessun revisore"; String relazione = request.getParameter("rel"); String titolo; String name; titolo = request.getParameter("titolo"); loguser = (String)(se.getValue("username")); name="relazioni.xml"; context.setAttribute("nome",name); context.setAttribute("autore",loguser); context.setAttribute("time",data); context.setAttribute("title",titolo); if(loguser.equals(null)){ out.println("<html>"); out.println("<BODY background=ovibos_moschatus01.jpg>"); out.println("<h1>La tua sessione è scaduta o non hai effettuato il login!!</h1>"); out.println("</body>"); out.println("</html>"); } else { // Creo la radice /**/File f = new File("C:\\jakarta-tomcat-3.3.2\\webapps\\prova2\\articoli\\relazioni.xml"); SAXBuilder saxbuilder = new SAXBuilder ( "org.apache.xerces.parsers.SAXParser",false ); /* ISTANZIAMO UN OGGETTO Document /**/ Document doc = null; try { /* COSTRUIAMO UN Document DA UN XML /**/ doc = saxbuilder.build(f); }catch (JDOMException e) { System.err.println ("errore: "+e); } Element root = doc.getRootElement(); /* ISTANZIAMO ALTRI OGGETTI Element PASSANDOGLI LE STRINGHE CHE SARANNO I NOMI DEI NOSTRI TAG /**/ Element newinitparam = new Element("relazione"); Element paramname = new Element("autore"); Element paramvalue = new Element("titolo"); Element testo = new Element("testo"); Element revisore = new Element("revisore"); /* ASSEGNAMO DEI VALORI DI TIPO "CDATA" AI TAG param-name E param-value: <param-name>nuovo nome</param-name> /**/ paramname.addContent(loguser); paramvalue.addContent(titolo); testo.addContent(relazione); revisore.addContent(rev); /* AGGIUNGIAMO I TAG param-name E Param-value AL TAG new-init-param /**/ newinitparam.addContent(paramname); newinitparam.addContent(paramvalue); newinitparam.addContent(testo); newinitparam.addContent(revisore); /* AGGIUNGIAMO IL TAG new-init-param AL SUPERTAG root /**/ root.addContent(newinitparam); try { FileOutputStream fos = new FileOutputStream("C:\\jakarta-tomcat-3.3.2\\webapps\\prova2\\articoli\\relazioni.xml",true); XMLOutputter outp = new XMLOutputter(); outp.setIndent(true); outp.setNewlines(true); outp.output(doc, fos); } catch (Exception e) {} } try {response.sendRedirect("http://localhost:8080/prova2/inserimento2");} catch(IOException e){ }} }
Grazie!!!!!!

Rispondi quotando