import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class Utenti_Crea {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc
= factory.
newDocumentBuilder().
parse(new File("Utenti.xml")); new Utenti_Crea().Utenti_Crea(doc, "Nome","Cognome","Citta", "Provincia","CAP","Email", "Notifica", "Username", "Password");
}
// leggo il nodo capo dell'xml che in questo caso è utenti
Element root
= doc.
getDocumentElement();
//Creo il nuovo elemento record
Element person
= doc.
createElement("Record");
//Creo elemento che contiene il nome
Element Nodo_nome
= doc.
createElement("Nome"); person.appendChild(Nodo_nome);
Text nome_testo = doc.createTextNode(Nome);
Nodo_nome.appendChild(nome_testo);
//Creo elemento che contiene il cognome
Element Nodo_cognome
= doc.
createElement("Cognome"); person.appendChild(Nodo_cognome);
Text cognome_testo = doc.createTextNode(Cognome);
Nodo_cognome.appendChild(cognome_testo);
//Creo elemento che contiene la citta
Element Nodo_citta
= doc.
createElement("Citta"); person.appendChild(Nodo_citta);
Text citta_testo = doc.createTextNode(Citta);
Nodo_citta.appendChild(citta_testo);
//Creo elemento che contiene la provincia
Element Nodo_provincia
= doc.
createElement("Provincia"); person.appendChild(Nodo_provincia);
Text provincia_testo = doc.createTextNode(Provincia);
Nodo_provincia.appendChild(provincia_testo);
//Creo elemento che contiene il cap
Element Nodo_cap
= doc.
createElement("Cap"); person.appendChild(Nodo_cap);
Text cap_testo = doc.createTextNode(Cap);
Nodo_cap.appendChild(cap_testo);
//Creo elemento che contiene l'email per contattare utente
Element Nodo_email
= doc.
createElement("Email"); person.appendChild(Nodo_email);
Text email_testo = doc.createTextNode(Email);
Nodo_email.appendChild(email_testo);
//Creo elemento che contiene la notifica email (forse opzionale)
Element Nodo_notifica
= doc.
createElement("Notifica"); person.appendChild(Nodo_notifica);
Text notifica_testo = doc.createTextNode(Notifica);
Nodo_notifica.appendChild(notifica_testo);
//Creo elemento che contiene il nome utente
Element Nodo_username
= doc.
createElement("Username"); person.appendChild(Nodo_username);
Text username_testo = doc.createTextNode(Username);
Nodo_username.appendChild(username_testo);
//Creo elemento che contiene la password
Element Nodo_password
= doc.
createElement("Password"); person.appendChild(Nodo_password);
Text password_testo = doc.createTextNode(Password);
Nodo_password.appendChild(password_testo);
//Attacco il tutto al nodo padre
Node firstNode = root.getFirstChild();
root.insertBefore(person, firstNode);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// Creo una stringa speciale dal documento-albero xml
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString
= sw.
toString();
//Scrittura sul file XML
out.write(xmlString)
out.close();
}
}