ciao
scusami farò come mi hai detto
ti ringrazio per avermi risposto solo che ho provato e non mi da nessun errore solo ke da lo stesso risultato al codice ke ho fatto io... ovvero nella pagina reccomendation.xml mi esce scritto questo
codice:
<?xml version="1.0" encoding="UTF-8"?>
<recommendations />
e non ho capito cosa sbaglio allora posto tutto il codice
codice:
/**
*
*/
package projectservlet;
/**
* @(#)Recommendations.java
*
*
* @author Raffaella
* @version 1.00 2007/4/24
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.io.File;
import java.util.*;
import java.text.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.util.Iterator;
import java.util.List;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.lang.String;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Recommendations extends HttpServlet {
Document documento = null;
Document itemDocumento = null;
Element elementoRate = null;
Element itemElemento = null;
String rating_inviato;
int countLine;
String id, author, description;
//metodo che permette di aggiungere un item copiandolo dal file Item.xml
public void aggiungiRatedItem(){
Calendar calendar = new GregorianCalendar();
String controllo = new String("ok");
//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATED
String itemPath = new String ("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/xml/Item.xml");
File itemFile = new File(itemPath);
//verifica che l'id scelto per essere valutato sia contenuto nel file Item.xml
//String tempIDPath = new String("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/temp/IDItem - " + calendar.get(Calendar.DAY_OF_MONTH) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR) + ".txt");
String tempIDPath = new String("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/temp/IDItem.txt");
File tempIDFile = new File(tempIDPath);
try{
//SE IL FILE ESISTE LO CARICO IN MEMORIA
SAXBuilder saxBuilderItem = new SAXBuilder();
try{
itemDocumento = saxBuilderItem.build(new File(itemPath));
}
catch (JDOMException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
BufferedReader readTempID = new BufferedReader( new FileReader(tempIDFile));
String line;
String tmp = "";
//Leggo tutto il file e lo butto in una variabile temporanea
while ((line = readTempID.readLine()) != null){
tmp += line;
}
readTempID.close();
Element elementoTempID = itemDocumento.getRootElement();
List lista_TempID = elementoTempID.getChildren();
// ottengo un iteratore alla lista chiamando il metodo iterator() di Collection (essendo una list una collection)
Iterator iteratoreTempID = lista_TempID.iterator();
while (iteratoreTempID.hasNext())
{
// ottengo l'elemento corrente chiamando next() sull'iteratore
Element tempID_corrente = (Element)iteratoreTempID.next();
String tempID = tempID_corrente.getAttributeValue("id");
if (tmp.equals(tempID)){
controllo = new String("ok");
break;
}
}
if (controllo.equals("ok"))
{
//provvedo a copiare ogni elemento
while (iteratoreTempID.hasNext())
{
// ottengo l'elemento corrente chiamando next() sull'iteratore
Element item_corrente = (Element)iteratoreTempID.next();
if ((item_corrente.getName()).equals("item"))
{id = item_corrente.getAttributeValue("id");}
if ((item_corrente.getName()).equals("author"))
{author = item_corrente.getText();}
if ((item_corrente.getName()).equals("description"))
{description = item_corrente.getText();}
//copia il content corrente, impostando i nuovi attributi, in un altro file xml
Element rated = new Element("ratedItem");
Element element_id = new Element("id");
Element element_rate = new Element("rate");
Element element_author = new Element("author");
Element element_description = new Element("description");
element_author.setText(author);
element_description.setText(description);
rated.addContent(element_author);
rated.addContent(element_description);
rated.setAttribute("id", id);
rated.setAttribute("rate", rating_inviato);
elementoRate.addContent(rated);
}
}
}
catch (FileNotFoundException ex){
System.err.println(ex);
}
catch (NumberFormatException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Calendar calendar = new GregorianCalendar();
StringBuffer buf = new StringBuffer();
response.setContentType("text/html");
PrintWriter responseOutput = response.getWriter();
buf.append("<html><head></head><body><table border=\"1\" width=\"40%\" align=\"center\"><tbody><tr>");
//preleva dal file ItemCount.txt il totale degli item da votare
//String tempIDPath = new String("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/temp/IDItem - " + calendar.get(Calendar.DAY_OF_MONTH) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR) + ".txt");
/* String tempIDPath = new String("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/temp/IDItem.txt");
File tempIDFile = new File(tempIDPath);
BufferedReader readTempID = new BufferedReader( new FileReader(tempIDFile));
String line2;
int countLine = 0;
//Leggo tutto il file e lo butto in una variabile temporanea
while ((line2 = readTempID.readLine()) != null){
countLine += 1;
}
readTempID.close();
for (int i = 1; i <= countLine; i++){
*/
//RECUPERO LE INFORMAZIONI INVIATE DAL FORM
rating_inviato = request.getParameter("rated");
if (rating_inviato != ""){
HttpSession session = request.getSession(true);
//String username = (String)session.getAttribute("Username");
//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATI
String path = new String ("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/xml/Recommendations.xml");
File file = new File(path);
//SE IL FILE NON ESISTE LO CREO E GLI AGGIUNGO L'ELEMENTO RADICE IL PRIMO ITEM CON RATING
if (!file.exists()){
file.createNewFile();
elementoRate = new Element("recommendations");
//AGGIUNGO IL RATING CHE RISULTA IL PRIMO DATO CHE IL FILE RECOMMENDATIONS.XML NON ESISTEVA
aggiungiRatedItem();
//SCRIVO IL DOCUMENT NEL FILE
documento = new Document(elementoRate);
XMLOutputter xmlOutputter = new XMLOutputter();
try{
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
xmlOutputter.output(documento, fileOutputStream);
buf.append("<td align=\"center\">
Items Rated.
<a href=\"index.htm\">Proceed
</a></p>
</td>");
}
catch (FileNotFoundException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
}
else{
//SE IL FILE ESISTE LO CARICO IN MEMORIA
SAXBuilder saxBuilder = new SAXBuilder();
try{
documento = saxBuilder.build(new File(path));
}
catch (JDOMException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
//RISCRIVO L'INTERO DOCUMENTO NEL FILE XML DOPO AVER AGGIUNTO IL RATING
aggiungiRatedItem();
XMLOutputter xmlOutputter = new XMLOutputter();
//IL METODO SEGUENTE Format.getPrettyFormat() SERVE PER SCRIVERE SUL FILE XML IN MODO BEN FORMATTATO
//CON GLI A CAPO E LE INDENTAZIONI PER OGNI ELEMENTO, ma non lo accetta
//xmlOutputter.setFormat(Format.getPrettyFormat());
try{
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
xmlOutputter.output(documento, fileOutputStream);
buf.append("<td align=\"center\">
Items Rated.
<a href=\"index.htm\">Proceed
</a></p>
</td>");
}
catch (FileNotFoundException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
}
}
else{
buf.append("<td align=\"center\">
Missing informations, try again
<a href=\"RecommendedItem.htm\">Go Back
</a></p>
</td>");
}
// }
buf.append("</tr></tbody></table></body></html>");
responseOutput.println(buf.toString());
responseOutput.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
ho sostituito il mio pezzo di codice in grassetto ovvero
codice:
// ottengo l'elemento corrente chiamando next() sull'iteratore
Element item_corrente = (Element)iteratoreTempID.next();
if ((item_corrente.getName()).equals("item"))
{id = item_corrente.getAttributeValue("id");}
if ((item_corrente.getName()).equals("author"))
{author = item_corrente.getText();}
if ((item_corrente.getName()).equals("description"))
{description = item_corrente.getText();}
//copia il content corrente, impostando i nuovi attributi, in un altro file xml
Element rated = new Element("ratedItem");
Element element_id = new Element("id");
Element element_rate = new Element("rate");
Element element_author = new Element("author");
Element element_description = new Element("description");
element_author.setText(author);
element_description.setText(description);
rated.addContent(element_author);
rated.addContent(element_description);
rated.setAttribute("id", id);
rated.setAttribute("rate", rating_inviato);
elementoRate.addContent(rated);
col tuo postato cioè
codice:
// ottengo l'elemento corrente chiamando next() sull'iteratore
Element item_corrente = (Element)itemIteratore.next();
String id = item_corrente.getAttributeValue("id");
//copia il content corrente, impostando i nuovi attributi, in un altro file xml
Element rated = new Element("ratedItem");
item_corrente.detach();
rated.addContent(item_corrente);
rated.setAttribute("id", id);
rated.setAttribute("rate", rating_inviato);
rated.detach(); //Questa linea credo non serva perche' l'element rated non ha parent
elementoRate.addContent(rated);
entrambi i codici danno lo stesso risultato ke ho scritto prima nel file .xml
perkè?
help please