I've developed a method to insert a new element in a XML File. I'm testing it reading the first element Usuario of the input file and appending it at the end.
Input:
<Usuarios>
<Usuario>
<id>identificador</id>
<email>[email protected]</email>
<rol>profesor</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
<Usuario>
<id>00000000H</id>
<email>[email protected]</email>
<rol>profesor</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
<Usuario>
<id>970104</id>
<email>[email protected]</email>
<rol>alumno</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
</Usuarios>
Output:
<Usuarios>
<Usuario>
<id>identificador</id>
<email>[email protected]</email>
<rol>profesor</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
<Usuario>
<id>00000000H</id>
<email>[email protected]</email>
<rol>profesor</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
<Usuario>
<id>970104</id>
<email>[email protected]</email>
<rol>alumno</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
<Usuario>
<id>identificador</id>
<email>[email protected]</email>
<rol>profesor</rol>
<alta>01/01/2012</alta>
<baja>30/08/2021</baja>
</Usuario>
</Usuarios>
What am I doing bad? There are two issues:
1.- The elements in the input are not properly indented y the output file. The transformer don't reindents all the registers? 2.- The new element is properly indented, but the new instance of not.
Ideas?
source:
public void almacenarUsuario(UsuarioNegocio usuario) throws Exception {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(directorio + "personas.xml"));
Element nUsuario = (Element) doc.getElementsByTagName("Usuarios").item(0);
Node node = doc.createElement("Usuario");
nUsuario.appendChild(node);
Element nid = doc.createElement("id");
nid.appendChild(doc.createTextNode(usuario.getIdUsuario()));
node.appendChild(nid);
Element nemail = doc.createElement("email");
nemail.appendChild(doc.createTextNode(usuario.getEmail()));
node.appendChild(nemail);
Element nrol = doc.createElement("rol");
nrol.appendChild(doc.createTextNode(usuario.getRol()));
node.appendChild(nrol);
Element nalta = doc.createElement("alta");
nalta.appendChild(doc.createTextNode(usuario.getFecha_alta()));
node.appendChild(nalta);
Element nbaja = doc.createElement("baja");
nbaja.appendChild(doc.createTextNode(usuario.getFecha_baja()));
node.appendChild(nbaja);
// Formatter //
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", new Integer(25));
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount","4");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/opt/icxp1/Temporal/jose/personal/nuevo.xml"));
transformer.transform(source, result);
}