PDA

View Full Version : Errore nella modifica di un file xml con jdom


boysna
24-07-2006, 19:30
Ragazzi ho un problema con la modifica di un file xml tramite jdom.

Purtroppo mi da questo errore "org.jdom.IllegalAddException: The element already has an existing parent "root"" quando vado ad aggiungere al root il nuovo elemento, quindi quando faccio la chiamata root.addContent(cliente).

Devo aggiungere un tag e per fare ciò uso la classe GestioneAcquisti:


public class GestioneAcquisti {

private String completePath, databasePath = "\\Database\\acquisti.xml";
Document acquisti;



public GestioneAcquisti(String applicationPath) {
try {
SAXBuilder builder = new SAXBuilder();
completePath = applicationPath + databasePath;
acquisti = builder.build(completePath);
}
catch (Exception e) {
e.printStackTrace();
}
}


//metodo principale
public void registraAcquisto(String login, Bean.PackProdotti carrello){

if(!haGiaAcquistato(login, carrello)){
//Codice per l'inserimento di un nuovo cliente
}



}


//Verifica se c'è gia un tag cliente con attributo login pari all'input
//Se c'è inserisce dei nuovi tag come figli del tag trovato
public boolean haGiaAcquistato(String login, Bean.PackProdotti carrello){

boolean bool = false;
Element root = acquisti.getRootElement();
List clientiList = root.getChildren("cliente");
Iterator clienti = clientiList.iterator();
Enumeration listaProdotti = carrello.listaProdotti();

while(clienti.hasNext()){
Element cliente = (Element)clienti.next();
String loginTemp = (String)cliente.getAttributeValue("login");
if(loginTemp.equals(login)){
while(listaProdotti.hasMoreElements()){
inserisciAcquistoUtente(cliente, (String[])listaProdotti.nextElement(), root);
}



}
}

return bool;


}


//Dopo aver trovato l'elemento a cui bisogna aggiungere i figli, si inseriscono i medesimi: le informazioni sono incluse in prodotto
public void inserisciAcquistoUtente(Element cliente, String[] prodotto, Element root){

Element acquisto = new Element("acquisto");
Element data = new Element("data");
Element prezzo = new Element("prezzo");
Element quantita = new Element("quantita");
Element taglia = new Element("taglia");
Element colore = new Element("colore");
Element marca = new Element("marca");
Element id = new Element("id");

data.setText("tempo");
prezzo.setText(prodotto[3]);
quantita.setText(prodotto[4]);
taglia.setText(prodotto[0]);
colore.setText(prodotto[1]);
marca.setText(prodotto[2]);
id.setText(prodotto[5]);

acquisto.addContent(data);
acquisto.addContent(prezzo);
acquisto.addContent(quantita);
acquisto.addContent(taglia);
acquisto.addContent(colore);
acquisto.addContent(marca);
acquisto.addContent(id);

cliente.addContent(acquisto);

root.addContent(cliente);

acquisti.setRootElement(root);

try{
XMLOutputter outputter = new XMLOutputter();
FileOutputStream output = new FileOutputStream(completePath);
outputter.output(acquisti, output);
}

catch (FileNotFoundException e) {
e.printStackTrace();
}

catch (IOException e) {
e.printStackTrace();
}


}
}


La struttura di acquisti.xml è questa:

<root>

<cliente login="mariorossi">
<acquisto>
<data>14/07/2006</data>
<prezzo>60</prezzo>
<quantita>2</quantita>
<taglia>43</taglia>
<colore>rosso</colore>
<marca>Nike</marca>
<id>00001</id>
</acquisto>
</cliente>
</root>