我想从pojo创建xml字符串。 我有一个以下pojo类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "companyType", "leadType" })
@XmlRootElement(name = "EnrichmentPayload", namespace = "http://www.example.org/EnrichmentPayload_LM_2.0")
public class Schema {
@XmlElement(name = "CompanyType", required = true)
CompanyType companyType;
@XmlElement(name = "LeadType", required = true)
LeadType leadType;
public CompanyType getCompanyType() {
return companyType;
}
public void setCompanyType(CompanyType companyType) {
this.companyType = companyType;
}
public LeadType getLeadType() {
return leadType;
}
public void setLeadType(LeadType leadType) {
this.leadType = leadType;
}
我有两个类CompanyType和LeadType,我也想要命名空间,
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="CompanyType")
public class CompanyType {
@XmlElement(required=true)
boolean threshold_met;
@XmlElement(required=true)
boolean isActive;
@XmlElement(required=true)
String company_id;
...
}
和潜在客户类型
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CompanyType")
public class LeadType {
@XmlElement(required = true)
boolean threshold_met;
@XmlElement(required = true)
int lead_id;
@XmlElement(required = true)
...
}
现在我正在以这种方式编组根元素:
JAXBContext jc = JAXBContext.newInstance(com.ss.dain.xmlcreator.Schema.class);
Marshaller marshaller = jc.createMarshaller();
//marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.org/EnrichmentPayload_LM_2.0");
// marshaller.setProperty("com.sun.xml.internal.bind.NameSpacePrefixMapper",
// new NameSpaceMapper());
StringWriter sw = new StringWriter();
marshaller.marshal(schema, sw);
xmlString = sw.toString();
在创建新实例时抛出异常
JAXBContext.newInstance(com.ss.dain.xmlcreator.Schema.class);