add element within another XML element DOM

2

this is my code:

    public class agregar extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException 
{
try {
    String nombre= request.getParameter("nombre");
    String constrasena= request.getParameter("constrasena");
    String tipo= request.getParameter("tipo");
     String grupo= request.getParameter("grupo");
    System.out.println(nombre);
    System.out.println(constrasena);
    System.out.println(tipo);
    System.out.println(grupo);

    String filepath = ("file.xml");
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = null;
    try {
        doc = docBuilder.parse(filepath);
    } catch (SAXException ex) {
        Logger.getLogger(agregar.class.getName()).log(Level.SEVERE, null, ex);
    }

     Node company = doc.getFirstChild();
    Node staff = doc.getElementsByTagName("grupo").item(1);       
    NamedNodeMap attr = staff.getAttributes();        
    Node nodeAttr = attr.getNamedItem("id");
    System.out.println("grupo"+nodeAttr.getTextContent());    
    // Get the root element      

    Element usuario = doc.createElement("usuario"); 
    usuario.setAttribute("id", ""+nombre+"");
    Element tip = doc.createElement("tipo");        
    tip.appendChild(doc.createTextNode(""+tipo+""));
    usuario.appendChild(tip);
    Element name = doc.createElement("userName");        
    name.appendChild(doc.createTextNode(""+nombre+""));
    usuario.appendChild(name);
    Element pass = doc.createElement("password");        
    pass.appendChild(doc.createTextNode(""+constrasena+""));
    usuario.appendChild(pass);        
    company.appendChild(usuario);


    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException ex) {
        Logger.getLogger(agregar.class.getName()).log(Level.SEVERE, null, ex);
    }
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(filepath));
    transformer.transform(source, result);

    System.out.println("Done");
    response.sendRedirect("admin");
} catch (ParserConfigurationException ex) {
    Logger.getLogger(agregar.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
    Logger.getLogger(agregar.class.getName()).log(Level.SEVERE, null, ex);
}

   }

    }

this is the xml that I want to edit:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><usuarios>
    <grupo id="2cm3">
    <usuario id="admin">
    <tipo>administrador</tipo>
    <userName>admin</userName>
    <password>admin</password>
    </usuario>
    </grupo>
    <grupo id="2cm4">
    <usuario id="admin">
      <tipo>administrador</tipo>
      <userName>admin</userName>
      <password>admin</password>
    </usuario>
    </grupo>
    </usuarios>

The problem is that this code does insert the node but inserts it like that

<?xml version="1.0" encoding="UTF-8" standalone="no"?><usuarios>
<grupo id="2cm3">
<usuario id="admin">
  <tipo>administrador</tipo>
  <userName>admin</userName>
  <password>admin</password>
</usuario>
</grupo>
<grupo id="2cm4">
<usuario id="admin">
  <tipo>administrador</tipo>
  <userName>admin</userName>
  <password>admin</password>
</usuario>
</grupo>
<usuario id="admin">   ----quiero todo esto dentro de 
  <tipo>administrador</tipo> ---- la etiqueta 
  <userName>admin</userName> -----grupo
  <password>admin</password>------pero lo esta poniendo afuera
</usuario>
</usuarios>

I hope and you can help me I've been trying for a while but nothing

    
asked by jonathan 26.11.2016 в 22:35
source

1 answer

1
  

I want all this inside   the label   group   but he's putting it out

After creating the Element usuario , you are adding it to company (which refers to <usuarios> ) on the line:

company.appendChild(usuario);


On the other hand, if you want to add it to the selected group staff (which refers to group 2, <grupo id="2cm4"> ) simply change the previous line by:

staff.appendChild(usuario);


Demo: link

    
answered by 27.11.2016 в 11:17