【问题标题】:Adding a new Object to the list of Objects based on the attribute of an Object inside the list in JAVA根据JAVA列表内对象的属性将新对象添加到对象列表中
【发布时间】:2021-10-24 10:21:11
【问题描述】:

我有一个显示的对象列表

Registry(Student=[Student(Gender=M, School=Hamburg, FirstName=RP, Value=null), 
                  Student(Gender=F, School=Berlin, FirstName=SK, Value=null),
                  Student(Gender=M, School=Frankfurt, FirstName=TK, Value=null)])

这表示解组后的 XML 结构。 XML的原始结构如下所示

<?xml version="1.0" encoding="UTF-8"?>
<Registry xmlns="http://www.registar.com" 
          xmlns:ms ="http://www.registar.com/ScoreVariant">
    <Student Gender = "M" School = "Hamburg">
        <FirstName>RP</FirstName>
    </Student>
    <Student Gender = "F" School = "Berlin">
        <FirstName>SK</FirstName>
    </Student>
    <Student Gender = "M" School = "Frankfurt">
        <FirstName>TK</FirstName>
    </Student>
</Registry>

有为 Registry、Student 和 Value 编写的类,带有 getter 和 setter 方法(使用 lombok 包)

现在,我要扫描对象列表,查找学校位置,如果位置是“柏林”,我想添加另一个学生。

    List<Registry> entries = new ArrayList<Registry>();
        try {


            File xmlFile = new File("MultipleNS.xml");
            JAXBContext jaxbContext;
            jaxbContext = JAXBContext.newInstance(Registry.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            Registry xmlentries = (Registry) jaxbUnmarshaller.unmarshal(xmlFile);
            entries.add(xmlentries);
    
    for (Registry e: entries) {
        for (Student s : e.getStudent()) {
            if (s.getSchool().equals("Berlin")) {
                Student obj = new Student();
                obj.setFirstName("MP");
                obj.setGender("F");
                obj.setSchool("Berlin");    // (1) 
            }                
         }           
       }
      }catch (JAXBException e) {
            e.printStackTrace();
        } catch (FactoryConfigurationError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        System.out.println("---------------------------------");
        
        ListIterator<Registry> litr = entries.listIterator();
        while (litr.hasNext()) {
            System.out.println(litr.next());
        }
    }

(1) 在这里我可以创建新对象,但我无法将其添加到 XML 中(因为注册表类具有 List&lt;Student&gt; student 作为属性。

最终,我想要如下所示的输出

Registry(Student=[Student(Gender=M, School=Hamburg, FirstName=RP, Value=null), 
                  Student(Gender=F, School=Berlin, FirstName=SK, Value=null),
                  Student(Gender=F, School=Berlin, FirstName=MP, Value=null), 
                  Student(Gender=M, School=Frankfurt, FirstName=TK, Value=null)])

任何建议或帮助将不胜感激? PS:初学者

【问题讨论】:

  • stackoverflow.com/questions/11624220/… 我觉得上面的帖子应该可以帮到你。
  • 答案对你有用吗?
  • 我用 for 循环本身完成了一个解决方法,不过我通读了链接
  • 我相信在完成整个解组过程之后在 main 方法中做这件事我认为不是一个明智的选择。最好使用aftermarshal,这样您就可以在获得它时进行处理。
  • 我真的无法理解那里的某些内容,但是如果您有示例示例,您可以在此链接中提供它吗?我想这对我有帮助 --stackoverflow.com/questions/68920060/…

标签: java xml jaxb unmarshalling moxy


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多