Ciao, sto cercando di modificare un file xml aggiungendo nodi col DOM, il codice è questo ma non so se va bene:
private void addInfo(String infoFile){
// factory initialisation
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// force the parser to validate the document
factory.setValidating(true);
Document document = null;
try {
// builder initialisation
DocumentBuilder builder = factory.newDocumentBuilder();
// launch the parsing and create DOM structure
document = builder.parse(infoFile);
} catch (Exception e) {
e.printStackTrace();
}
if (document != null) {
NodeList listaFoto= document.getElementsByTagName("image");
int numeroFoto= listaFoto.getLength();
for(int i=0; i<numeroFoto; i++){//se eseste non faccio niente
if((listaFoto.item(i).getFirstChild().getNodeValue ().equals(fileName))==true) return;
}//aggiugo info
Element foto = document.createElement("foto");
Element image = document.createElement("image");
Element thumbnail = document.createElement("thumbnail");
Text im=document.createTextNode(fileName);
Text tb=document.createTextNode("TB_"+fileName);
image.appendChild(im);
thumbnail.appendChild(tb);
foto.appendChild(image);
foto.appendChild(thumbnail);
Node root=document.getElementsByTagName("user").item(0) ;
root.appendChild(foto);
}
}
ora volevo sapere come faccio a salvare il file, ho trovato dei tutorial che parlano solo di sax, sapete dirmi qualcosa? grazie e ciao