java dom modify xml file

I want to add some nodes into an existing xml file. The xml file structure looks like:

<?xml version="1.0" encoding="utf-8" standalone="no"?><?xml-stylesheet type="text/xsl" href="new2.xsl"?><patients>
    <patient>
    <stoixeia_astheni>
        <arithmos_eksetasis>1</arithmos_eksetasis>
        <imerominia_eksetasis>11/12/2005</imerominia_eksetasis>
        <amka>14385</amka>
        <surname>aaa</surname>
        <name>aaa</name>
        <onoma_patros>aaa</onoma_patros>
        <imgennisis>15/03/1984</imgennisis>
        <diagnosi>aaa</diagnosi>
        <famagogi>depon</famagogi>
    </stoixeia_astheni>
    <stoixeia_epikoinonias>
        <dieuthinsi>aaa</dieuthinsi>
        <takodikas>11474</takodikas>
        <perioxi>aaa</perioxi>
        <stathero>2106425246</stathero>
        <kinito>-</kinito>
    </stoixeia_epikoinonias>
    <loipa_stoixeia>
        <fylo>aaa</fylo>
        <oikkatastasi>aaa</oikkatastasi>
        <epaggelma>aaa</epaggelma>
        <istoriko>-</istoriko>
        <sxolia>-</sxolia>
    </loipa_stoixeia>
    </patient>
    <patient>
    <stoixeia_astheni>
        <arithmos_eksetasis>2</arithmos_eksetasis>
        <imerominia_eksetasis>12/12/2005</imerominia_eksetasis>
        <amka>14325</amka>
        <surname>aaa</surname>
        <name>aaa</name>
        <onoma_patros>aaa</onoma_patros>
        <imgennisis>15/03/1984</imgennisis>
        <diagnosi>aaa</diagnosi>
        <famagogi>depon</famagogi>
    </stoixeia_astheni>
    <stoixeia_epikoinonias>
        <dieuthinsi>aaa</dieuthinsi>
        <takodikas>11474</takodikas>
        <perioxi>aaa</perioxi>
        <stathero>2106425246</stathero>
        <kinito>-</kinito>
    </stoixeia_epikoinonias>
    <loipa_stoixeia>
        <fylo>aaa</fylo>
        <oikkatastasi>aaa</oikkatastasi>
        <epaggelma>aaa</epaggelma>
        <istoriko>-</istoriko>
        <sxolia>-</sxolia>
    </loipa_stoixeia>
    </patient>
</patients>

I am trying something like:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String[] elem;
    elem = new String[20];
    elem [1]="arithmos_eksetasis";
    elem [2]="imerominia_eksetasis";
    elem [3]="amka";
    elem [4]="surname";
    elem [5]="name";
    elem [6]="onoma_patros";
    elem [7]="imgennisis";
    elem [8]="diagnosi";
    elem [9]="famagogi";
    elem [10]="dieuthinsi";
    elem [11]="takodikas";
    elem [12]="perioxi";
    elem [13]="stathero";
    elem [14]="kinito";
    elem [15]="fylo";
    elem [16]="oikkatastasi";
    elem [17]="epaggelma";
    elem [18]="istoriko";
    elem [19]="sxolia";

    try {
        String filepath = "C:\Users\Chris\Desktop\tsiou\workspace\askhsh3\WebContent\askisi3.xml";

        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);


        //Node staff = doc.getElementsByTagName("staff").item(0);


        // append a new node to staff
        Document doc2 = docBuilder.newDocument();
        Element patient = doc2.createElement("patient");

        Element st_as = doc2.createElement("stoixeia_astheni");
        for(int i=1;i<=9;i++){
            Element tmp= doc2.createElement(elem[i]);
            tmp.appendChild(doc2.createTextNode("aaa"));
            st_as.appendChild(tmp);
        }
        Element st_ep = doc2.createElement("stoixeia_epikoinonias");
        for(int i=10;i<=15;i++){
            Element tmp= doc2.createElement(elem[i]);
            tmp.appendChild(doc2.createTextNode("aaa"));
            st_ep.appendChild(tmp);     
        }
        Element st_lp = doc2.createElement("loipa_stoixeia");
        for(int i=16; i<=19;i++){
            Element tmp= doc2.createElement(elem[i]);
            tmp.appendChild(doc2.createTextNode("aaa"));
            st_lp.appendChild(tmp);     
        }
        patient.appendChild(st_as);
        patient.appendChild(st_ep);
        patient.appendChild(st_lp);
        doc.importNode(patient,true);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        OutputStream out = new FileOutputStream(filepath);
        trans.transform(new DOMSource(doc), new StreamResult(out));

       } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }


    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();

}

The result is with no luck (the xml doesn’t changed).

When i am trying trans.transform(new DOMSource(patient), new StreamResult(out)); i can see the new patient..i also tried doc.adoptNode(patient)

Any advice would be usefull

Answer

In place of doc.importNode(patient,true);, Try this:

//Find existing parent node
Node patientsNode = doc.getElementsByTagName("patients").item(0);

//append the new child node
patientsNode.appendChild(patient); 

I am using similar code and it works perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *