首页 \ 问答 \ 使用JDOM创建具有嵌套元素的XML文件(Creating XML file with nested elements using JDOM)

使用JDOM创建具有嵌套元素的XML文件(Creating XML file with nested elements using JDOM)

我使用JDOM创建了一个XML文件(来自Object)并且它可以工作,但我无法让一个嵌套元素(教师)正常工作。

以下是创建文件的方法:

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

我在我创建的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>

它看起来应该是这样的:

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

任何人都可以告诉我如何实现这一目标? 谢谢...


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


原文:https://stackoverflow.com/questions/21317469
更新时间:2021-12-24 06:12

最满意答案

Gitolite g2-g3迁移页面提到:

现在转到您的旧克隆,然后推送它:

$ cd old.ga
$ gitolite push -f
    ...usual git progress output deleted...
remote: FATAL: git config foo.bar not allowed
remote: check GIT_CONFIG_KEYS in the rc file
To /home/git/repositories/gitolite-admin.git
 + 7eb8163...1474770 master -> master (forced update)

AAHA! 我忘了在新的rc文件中设置GIT_CONFIG_KEYSGIT_CONFIG_KEYS新名称),所以修复:

$ vim ~/.gitolite.rc
(edit and set it to `.*` for now)

确保你的.gitolite.rc确实包含:

GIT_CONFIG_KEYS             =>  '.*',

(在这个GitLab问题中也提到过,即使它不是问题的直接根本原因)


我有另一个问题是我的projects.list文件在我向gitolite-admin conf添加新的存储库时没有自动更新。
对于gitweb,我不得不手动编辑该文件。 你知道可能导致什么吗?

与外部工具接口 ”页面提到:

gitweb可读repos列表被写入一个文件,其名称由rc文件变量GITWEB_PROJECTS_LIST
如果未指定或为空,则此变量的默认值为$HOME/projects.list

确保你的.gitolite.rc

GITWEB_PROJECTS_LIST        => '/path/to/projects.list',

The Gitolite g2-g3 migration page mentions:

Now go to your old clone, and push it:

$ cd old.ga
$ gitolite push -f
    ...usual git progress output deleted...
remote: FATAL: git config foo.bar not allowed
remote: check GIT_CONFIG_KEYS in the rc file
To /home/git/repositories/gitolite-admin.git
 + 7eb8163...1474770 master -> master (forced update)

Aaha! I forgot to set GIT_CONFIG_KEYS (new name for GL_GITCONFIG_KEYS) in the new rc file so fix that:

$ vim ~/.gitolite.rc
(edit and set it to `.*` for now)

Make sure your .gitolite.rc does contain:

GIT_CONFIG_KEYS             =>  '.*',

(It was also mentioned in this GitLab issue, even though it wasn't the direct root cause of the issue)


I got another issue which is my projects.list file does not get updated automatically when I added a new repository to gitolite-admin conf.
For gitweb, I had to manually edited that file. Did you know what could possibly cause that?

The "interfacing with external tools" page mentions:

The list of gitweb-readable repos is written to a file whose name is given by the rc file variable GITWEB_PROJECTS_LIST.
The default value of this variable, if it is not specified or empty, is $HOME/projects.list.

Make sure you have in your .gitolite.rc:

GITWEB_PROJECTS_LIST        => '/path/to/projects.list',

相关问答

更多
  • 您没有在自述文件中看到“ git@xxx:/home/git/repositories/foo.git ”网址。 你看到的是: git@xxx:foo 换句话说,您不能指定完整路径。 另外,请确保不要删除本地repo gitolite-admin 。 如果你有一个新的repo foo ,你必须在其他地方克隆它: git clone git@xxx:foo 我怀疑你只有一个ssh公钥/私钥,但如果你必须维护多个身份,请参阅“ 一台机器上的多个gitolite用户 ”。 You don't see "git ...
  • 新密钥在什么时候被添加到.ssh授权密钥文件中? 推送后,立即提供您的密钥: 是一个公钥(一行类似于ssh-rsa AAAAB3NzaC1yc2EAA...hFvETUVDvF2iZ ) 是访问gitolite: tom.pub的资源名称后面的名称 正如OP Dee 在评论中确认的那样 ,问题在于代表用户的公钥文件名: 唯一的错误是错误的用户名,我认为这是因为我在一组用户的名字中使用了' / ' 实际上, 用户名模式是: ^\@?[0-9a-zA-Z][-0-9a-zA-Z._\@+]*$ 不允许' / ...
  • 在你的机器上执行ssh -vvvv git@gitserver.com 。 你会看到提供什么密钥。 现在在这个用户的机器上做同样的事情。 另外请将你的配置文件发布到gitolite上,如果你有它们,那么〜/ .ssh / config文件,如果你们有它们的话。 用一般的东西替换你的敏感回购。 (upvoted你的问题) do a ssh -vvvv git@gitserver.com on your machine. You will see what keys are offered. Now do th ...
  • URL正面的gitolite @实际上并不是您使用SSH时的用户。 它是服务器上的服务(我假设)所以gitolite @ localhost:my-repo完全有效。 在使用GIT时,您将始终看到对git @的引用。 您的服务是gitolite,因此将始终使用gitolite @。 Git服务器使用SSH公钥进行身份验证,因此您创建的密钥允许用户连接到服务器。 该密钥也与该用户有关,因此当使用该密钥时,假设用户发出诸如提交之类的git命令。 从侧面说明,当我第一次使用我们的GIT系统时,一位同事推荐http ...
  • 使用gitolite ,所有ssh通信都使用用于安装gitolite的帐户完成。 在你的情况下: gitolite 。 但是,您可以指定不同的公钥,以指示gitolite对其他用户进行身份验证。 ssh会话仍将以gitolite形式执行。 但传递给gitolite脚本的名称将被testid (因为公钥在gitolite中以~/.ssh/authorized_keys为' testid ') 所以使用~testid/.ssh/config文件,在其中提到正确的参数: Host gitolitesrv Host ...
  • git clone myname@myserver:/home/git/repositories/testing.git 完全绕过没有被称为的gitolite 使用错误的帐户'myname',该帐户无权阅读git帐户文件夹。 应该做的是: git clone git@myserver:test_repo.git or git clone git@myserver/test_repo.git 你不应该指定'repositories'文件夹(gitolite知道repo应该在哪里) 假设ssh -Tv ...
  • Gitolite g2-g3迁移页面提到: 现在转到您的旧克隆,然后推送它: $ cd old.ga $ gitolite push -f ...usual git progress output deleted... remote: FATAL: git config foo.bar not allowed remote: check GIT_CONFIG_KEYS in the rc file To /home/git/repositories/gitolite-admin.git + 7eb ...
  • 我通过阅读desc命令的代码找到了所遗漏的内容(在doc中没有看到) git@host $ nano ~/.gitolite.rc %RC = ( [...] WRITER_CAN_UPDATE_DESC => 1, [...] ) I found what was missing by reading the code of the desc command (didn't see it in the doc) git@host $ nano ~/.gitoli ...
  • 我很高兴现在说它是固定的。 我在gitolite post_compile部分添加了这个https://github.com/sitaramc/gitolite/blob/master/src/triggers/post-compile/update-description-file脚本。 1.打开HOME目录中的.gitolite.rc文件并编辑post_compile部分。 POST_COMPILE => [ 'post-compile/ss ...
  • 我实际上不需要gitweb规则或project.list在我的gitweb设置中完成: 我只确保我有一个gitweb.conf.pl : 将由gitweb调用(通过gitweb_config.perl文件,如果gitweb检测到它存在,则调用该文件) 将调用gitolite来查看是否可以授予或应该拒绝访问回购。 I don't actually need gitweb rules or project.list to be complete in my gitweb setup: I only make s ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)