mardi 2 février 2016

XML loop not putting contents inside node

I am trying to achieve the following:

<CTS>
<startTime>00:00:00</startTime>
<endTime>00:00:00</endTime>
</CTS>

As you can see the start time and end time sit inside the CTS,

This is my code

 if (headerString.equals("startTime") || headerString.equals("endTime")){

                   Element headerElement1 = document.createElement("CTS");  

                   Element st = document.createElement("startTime");//create a new element
                  Element et = document.createElement("endTime");//create a new element


          if (headerString.equals("startTime")){         

        Text st1 = document.createTextNode(s);//contents of the node
        st.appendChild(st1);//apend the text contents 
        headerElement1.appendChild(st);
        productElement.appendChild(headerElement1);

                 index++;         
          }   


            if (headerString.equals("endTime")){         

        Text et1 = document.createTextNode(s);//contents of the node
        et.appendChild(et1);//apend the text contents
        headerElement1.appendChild(et);
        productElement.appendChild(headerElement1);

                 index++;         
          } 

however what i am getting is:

<CTS>
<startTime>01:00:00</startTime>
</CTS>
<CTS>
<endTime>05:00:00</endTime>
</CTS>

I am therefore creating the element CTS twice one for startTime and one for the endTime, how do i change my code so that i get what i am trying to achive?

Aucun commentaire:

Enregistrer un commentaire