majeno de XML
Publicado por Federico (2 intervenciones) el 12/04/2011 15:53:05
NO se que pasa en esta función. resulta que cargo un xml Document,( corecto por que lo pouedo manjar entero) busco con xpath un element, ( correcto por que puedo ver cada todos los atributos y son los que buscaba), creo un nuevo elemento,y trato de agregarlo , pero al guardar no me graba los cambios. la funcion grabar está bien, por que si el nuevo elemento se lo agrego al root element todo funciona correctamente doc.getrootelement.addcontent(nuevo) y guardo y si re refleja los cambios. Estas son las funciones a ver si alguien sabe como se hace
Document d;
private void cargaDoc() throws JDOMException, IOException{
SAXBuilder builder=new SAXBuilder(false);
//usar el parser Xerces y no queremos
//que valide el documento
d=builder.build("alumnos.xml");
}
public void addAnotacionAlumno(String nombre, String apellidos,String fecha, String motivo) throws JDOMException, IOException{
cargaDoc();
String id=null;
//busco el alumno y le añado la anotacion
Element r = (Element)XPath.selectSingleNode(d,"//alumno[@nombre=\"" + nombre + "\"" +
" and @apellidos=\"" + apellidos + "\"]");
Element nuevo = new Element("anotacion");
nuevo.setAttribute("fecha",fecha);
nuevo.setAttribute("descripcion",motivo);
id = String.valueOf(maxIdAnotaciones()+1);
nuevo.setAttribute("id","5");
r.addContent(nuevo);
guardar();
}
private void guardar() throws IOException{
XMLOutputter out=new XMLOutputter();
FileOutputStream file=new FileOutputStream("alumnos.xml");
out.output(d,file);
file.flush();
file.close();
}
Document d;
private void cargaDoc() throws JDOMException, IOException{
SAXBuilder builder=new SAXBuilder(false);
//usar el parser Xerces y no queremos
//que valide el documento
d=builder.build("alumnos.xml");
}
public void addAnotacionAlumno(String nombre, String apellidos,String fecha, String motivo) throws JDOMException, IOException{
cargaDoc();
String id=null;
//busco el alumno y le añado la anotacion
Element r = (Element)XPath.selectSingleNode(d,"//alumno[@nombre=\"" + nombre + "\"" +
" and @apellidos=\"" + apellidos + "\"]");
Element nuevo = new Element("anotacion");
nuevo.setAttribute("fecha",fecha);
nuevo.setAttribute("descripcion",motivo);
id = String.valueOf(maxIdAnotaciones()+1);
nuevo.setAttribute("id","5");
r.addContent(nuevo);
guardar();
}
private void guardar() throws IOException{
XMLOutputter out=new XMLOutputter();
FileOutputStream file=new FileOutputStream("alumnos.xml");
out.output(d,file);
file.flush();
file.close();
}
Valora esta pregunta
0