Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jaxb-将元素添加到列表(maven插件)_Java_Xml_Maven_Jaxb - Fatal编程技术网

Java jaxb-将元素添加到列表(maven插件)

Java jaxb-将元素添加到列表(maven插件),java,xml,maven,jaxb,Java,Xml,Maven,Jaxb,我用jaxb生成了我的类,现在我需要填充一些列表。最好的方法是什么 pom.xml: <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <executions> <exe

我用jaxb生成了我的类,现在我需要填充一些列表。最好的方法是什么

pom.xml:

         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/META-INF/xsd</schemaDirectory>
                    <packageName>be.structure</packageName>
                <outputDirectory>${basedir}/target/generated/java</outputDirectory>
            </configuration>
        </plugin>

org.codehaus.mojo
jaxb2 maven插件
xjc
${basedir}/src/main/resources/META-INF/xsd
结构
${basedir}/target/generated/java
列表所在的生成类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "configuration", propOrder = {
"professions"
})
public class Configuration {

protected List<Profession> professions;

public List<Profession> getProfessions() {
    if (professions == null) {
        professions = new ArrayList<Profession>();
    }
    return this.professions;
    }
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=“configuration”,比例器={
“职业”
})
公共类配置{
受保护职业名单;
公开名单{
if(职业==null){
职业=新的ArrayList();
}
把这个还给我;
}
}

但是正如你所看到的,没有“addprofessions”或“setProfessions()”之类的东西。我知道有办法,但我真的记不起来了

getProfessions().add(profession)
如果基础列表是可变的,那么应该可以做到这一点,但是通常您不会更改JAX实例的内容,因为JAXB基于读取的XML数据为您填充对象-如果您更改该列表,它将不再表示XML。

确实如此。。哈哈。我这样做是因为这是一项非常特殊的业务。“通常情况下,您不会更改JAX实例的内容”-这没有意义。当然,您可以更改JAXB模型bean来构建一个模型,然后将其封送到XML。而且,对文档进行解组、修改模型然后封送修改后的模型也是完全合法的。