1

I am having problems creating an xml doc with the hierarchy I am looking for ..

<claimants>
<claimant>
    <Area>Discrimination/Equality</Area>
    <Complaints>1</Complaints>
    <AssocatedComplaints>3</AssocatedComplaints>
 </claimant>
 <claimant>
    <Area>Redundancy</Area>
    <Complaints>1</Complaints>
    <AssocatedComplaints>3</AssocatedComplaints>
 </claimant>

I am getting this -

<claimants>
<claimant>
    <Area>Discrimination/Equality</Area>
    <Complaints>1</Complaints>
    <AssocatedComplaints>3</AssocatedComplaints>
    <Area>Redundancy</Area>
    <Complaints>1</Complaints>
    <AssocatedComplaints>3</AssocatedComplaints>
    <Area>Terms and Conditions of Employment</Area>
    <Complaints>1</Complaints>
    <AssocatedComplaints>3</AssocatedComplaints>
</claimant>

Here is the relevant part of the code, appendchild does not work as I would have expected, I have tried a couple of other options, how can I get the claimant element to populate and repeat ??

Document document = new DocumentImpl();
Element claimants = document.createElement("claimants");
Element claimant = document.createElement("claimant");

... ...

rs = ps.executeQuery();
while (rs.next())  {
    // get data in
    String area = rs.getString("Area");
    System.out.println(">> Area : "+area);
    complaints = rs.getString("Complaints");
    System.out.println(">> Complaints : "+ complaints);
    associated = rs.getString("Associated");
    System.out.println(">> Associated : "+associated);

    // write text elements
    Element eArea = document.createElement("Area");
    eArea.appendChild(document.createTextNode(area));
    Element eComplaints = document.createElement("Complaints");
    eComplaints.appendChild(document.createTextNode(complaints));
    Element eAssociated = document.createElement("AssocatedComplaints");
    eAssociated.appendChild(document.createTextNode(associated));

    // append to claimant 
    System.out.println("appending ELEMENTS to claimant");
    claimant.appendChild(eArea);
    claimant.appendChild(eComplaints);
    claimant.appendChild(eAssociated);

    // append to claimants
    System.out.println("appending claimant to claimants");
    claimants.appendChild(claimant);
    //claimants.insertBefore(claimant, claimants.firstChild);

    System.out.println("creating new element claimant");
    Element claimant = document.createElement("claimant");
    }

document.appendChild(claimants);

thanks for any help ..

1
  • That's not Javascript (in a browser). Is it JScript? Commented Aug 18, 2012 at 23:25

1 Answer 1

3

You need to create a new claimaint for each iteration, inside the loop:

while (rs.next())  {
  Element claimant = document.createElement("claimant"); // a new claimaint is created for each new row

  // append elements to claimant
  claimant.appendChild(eArea);
  claimant.appendChild(eComplaints);
  claimant.appendChild(eAssociated);

  // append to claimaints
  claimants.appendChild(claimant);
}
Sign up to request clarification or add additional context in comments.

Comments

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.