简体   繁体   English

使用JDOM创建带有嵌套元素的XML文件

[英]Creating XML file with nested elements using JDOM

I created an XML file (from Object) using JDOM and it works, but I can't get one nested element (Teachers) to work properly. 我使用JDOM创建了一个XML文件(来自Object),它可以工作,但是我无法使一个嵌套元素(老师)正常工作。

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文件中得到了这个:

<?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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM