0

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);
}
2
  • 1
    Why do you care about indentation? That doesn't make the XML invalid in any way, does it? Commented Jul 6, 2012 at 11:13
  • 1
    It must be read by persons and indentation helps... Commented Jul 6, 2012 at 11:21

1 Answer 1

3

Before transforming,

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Sign up to request clarification or add additional context in comments.

3 Comments

It is there... code 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("{xml.apache.org/xalan}indent-amount","4"); DOMSource source = new DOMSource(doc); code
why do you set the indent amount to 4, when it's 8 in the original document?
There is no particular reason, yet the code does not work properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.