1

探している階層で xml ドキュメントを作成する際に問題が発生しています..

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

私はこれを得ています -

<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>

コードの関連部分は次のとおりです。appendchild は期待どおりに機能しません。他のいくつかのオプションを試しましたが、請求者要素を取得して繰り返すにはどうすればよいですか??

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);

助けてくれてありがとう..

4

1 に答える 1

3

ループclaimaintで、反復ごとに新しいものを作成する必要があります。

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);
}
于 2012-08-18T23:25:55.937 に答える