JAXB annotated class:
JAXB注释类:
@XmlRootElement(name = "group")
@XmlType(propOrder = {"name", "description", "types" })
public class GroupElement implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private String description;
private String name;
private List<TypeElement> types;
@XmlTransient
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@XmlElement(name = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "type")
public List<TypeElement> getTypes() {
return types;
}
public void setTypes(List<TypeElement> types) {
this.types= types;
}
}
java-package.info
@XmlSchema(namespace = "http://www.test.at/r1/v1" ,
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns={@XmlNs(prefix="cdr1", namespaceURI="http://www.test.at/r1/v1")})
JAXB marshalling:
JAXBContext jaxbContext = JAXBContext.newInstance(Group.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(group, sw);
I need to set the namespace dynamically depending on the group. With the package-info approach I get the same namespace in every XML file.
我需要根据组动态设置命名空间。使用package-info方法,我在每个XML文件中都获得相同的命名空间。
I also tried
我也试过了
JAXBElement<Group> element = new JAXBElement<Group>(new
QName(latestStructure.getNamespace(), "group", "cdr1"),
Group.class, group);
But With this approach only the root element gets the namespace and prefix assigned.
但是使用这种方法,只有root元素才能获得命名空间和前缀。
Does anyone have an idea how to do this?
有谁知道如何做到这一点?
1 个解决方案
#1
0
You could remove namespace info from the class and package, but create object factories with qualified names (so as many object factories as many namespaces),
您可以从类和包中删除命名空间信息,但是创建具有限定名称的对象工厂(以便许多对象工厂具有多个命名空间),
@XmlRegistry
public class ObjectFactory {
...
}
and then use the correct factory to initiate your context instead of the Group class:
然后使用正确的工厂来启动您的上下文而不是Group类:
JAXBContext.newInstance(ObjectFactory.class);
For me easier would be to again use beans without namespaces, marshall to namespace free xml and transform the xml using xslt to add correct "dynamic" namespace as required.
对我来说,更容易再次使用没有命名空间的bean,marshall命名空闲的xml并使用xslt转换xml以根据需要添加正确的“动态”命名空间。
#1
0
You could remove namespace info from the class and package, but create object factories with qualified names (so as many object factories as many namespaces),
您可以从类和包中删除命名空间信息,但是创建具有限定名称的对象工厂(以便许多对象工厂具有多个命名空间),
@XmlRegistry
public class ObjectFactory {
...
}
and then use the correct factory to initiate your context instead of the Group class:
然后使用正确的工厂来启动您的上下文而不是Group类:
JAXBContext.newInstance(ObjectFactory.class);
For me easier would be to again use beans without namespaces, marshall to namespace free xml and transform the xml using xslt to add correct "dynamic" namespace as required.
对我来说,更容易再次使用没有命名空间的bean,marshall命名空闲的xml并使用xslt转换xml以根据需要添加正确的“动态”命名空间。