문제

I created an XML file (from Object) using JDOM and it works, but I can't get one nested element (Teachers) to work properly.

Here is the method that creates the file:

public void writeFileUsingJDOM(List<Activity> activityList, String fileName) throws IOException {

        Document doc = new Document();
        doc.setRootElement(new Element("Activities", Namespace.getNamespace("http://www.jana.com/activities")));
        for(Activity act : activityList) {
            Element activity = new Element("Activity");
            activity.setAttribute("id", act.getActivityId().toString());
            activity.addContent(new Element("ActivityDescription").setText(act.getActivityDescription()));
            activity.addContent(new Element("CourseDescription").setText(act.getCourse().getCourseDescription()));
            // retrieve the list of teachers based on activity id
            List<Teacher> teacherList = teacherService.getAll(act.getActivityId());
            activity.addContent(new Element("Teachers")); // THIS IS WRONG!
            for(Teacher teach : teacherList) {
                activity.addContent(new Element("Teacher").addContent(new Element("Name").setText(teach.getFirstName() + " " + teach.getLastName())));
            }
            activity.addContent(new Element("SubmissionDate").setText(act.getSubmissionDate()));
            activity.addContent(new Element("Score").setText(act.getScore().toString()));
            activity.addContent(new Element("Note").setText(act.getNote()));
            doc.getRootElement().addContent(activity);
        }
        //write JDOM document to file
        XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
        xmlOutputter.output(doc, new FileOutputStream(fileName));
    }

I get this in my created XML file:

<?xml version="1.0" encoding="UTF-8"?>
  ...
  <Activity xmlns="" id="82">
    <ActivityDescription>Some other activity</ActivityDescription>
    <CourseDescription>Some other course</CourseDescription>
    <Teachers /> <!-- THIS IS THE PROBLEM! Teacher elements should be inside Teachers element -->
    <Teacher>
      <Name>Douglas Richardson</Name>
    </Teacher>
    <Teacher>
      <Name>Kieran Pittman</Name>
    </Teacher>
    <SubmissionDate>01/21/2014</SubmissionDate>
    <Score>30</Score>
    <Note>Some other note</Note>
  </Activity>
  ...
</Activities>

And it should look like this:

<Teachers>
   <Teacher>
      <Name>Douglas Richardson</Name>
    </Teacher>
    <Teacher>
      <Name>Kieran Pittman</Name>
    </Teacher>
</Teachers>

Can anyone please tell me how to achieve this? Thank you...

도움이 되었습니까?

해결책

You need to hook those teachers to the right Element. Try something like this.

Element el = new Element("Teachers");
activity.addContent(el);
for(Teacher teach : teacherList) {
    el.addContent(new Element("Teacher").addContent(new Element("Name").setText(teach.getFirstName() + " " + teach.getLastName())));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top