所有子元素都用名称空间[Spring Batch&JAXB]标记
我想使用Spring批处理JAXB2Marshaller生成XML文件,但我在下面的问题上坚持了一周,提前谢谢 预期的xml所有子元素都用名称空间[Spring Batch&JAXB]标记,jaxb,spring-batch,Jaxb,Spring Batch,我想使用Spring批处理JAXB2Marshaller生成XML文件,但我在下面的问题上坚持了一周,提前谢谢 预期的xml <?xml version="1.0" encoding="UTF-8"?> <p:ContactList xmlns:p="test:ns:2012"> <p:CompanyInfo> <p:CompanyName>Oracle</p:CompanyName> <p:Departmen
<?xml version="1.0" encoding="UTF-8"?>
<p:ContactList xmlns:p="test:ns:2012">
<p:CompanyInfo>
<p:CompanyName>Oracle</p:CompanyName>
<p:DepartmentInfo>
<p:DepartmentName>Java</p:DepartmentName>
</p:DepartmentInfo>
</p:CompanyInfo>
</p:ContactList>
ObjectFactory.java
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name = "CompanyInfo")
public JAXBElement<CompanyInfo> createContactList(CompanyInfo value) {
return new JAXBElement<CompanyInfo>(null, CompanyInfo.class, null, value);
}
}
DepartmentInfo.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DepartmentInfo", propOrder = {
"departmentName"
})
public class DepartmentInfo {
@XmlElement(name = "DepartmentName", required = true)
protected String departmentName;
}
通过在NamespacePrefixMapper中添加getContextualNamespaceDecls解决了问题
@Override
public String[] getContextualNamespaceDecls() {
return new String[] {"p", "test-ns-2012" };
}
在您的应用程序中,哪些因素起作用?在某处创建没有名称空间的JAXBElement,是否有可能导致根元素失去名称空间限定?非常感谢,新年快乐。rootTagName是org.springframework.batch.item.xml.statxeventitemwriter的属性,它将在startDocument方法writer.addfactory.createStartElement、getRootTagName中写入根元素;除了在ObjectFactory中,我没有在我的应用程序中添加任何JAXBElement,返回新的JAXBElementnew QNamep,test-ns-2012,CompanyInfo.class,null,value;
@javax.xml.bind.annotation.XmlSchema(namespace = "test:ns:2012", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
xmlns={@XmlNs(prefix="p", namespaceURI="test:ns:2012")})
package org.springframework.batch.contactlist;
import javax.xml.bind.annotation.XmlNs;
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(name = "CompanyInfo")
public JAXBElement<CompanyInfo> createContactList(CompanyInfo value) {
return new JAXBElement<CompanyInfo>(null, CompanyInfo.class, null, value);
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="CompanyInfo")
@XmlType(name = "CompanyInfo", propOrder = {
"companyName",
"departmentInfo"
})
public class CompanyInfo {
@XmlElement(name = "CompanyName")
protected String companyName;
@XmlElement(name = "DepartmentInfo", required = true)
protected DepartmentInfo departmentInfo;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DepartmentInfo", propOrder = {
"departmentName"
})
public class DepartmentInfo {
@XmlElement(name = "DepartmentName", required = true)
protected String departmentName;
}
@Override
public String[] getContextualNamespaceDecls() {
return new String[] {"p", "test-ns-2012" };
}