Java 没有XmlRootElement注释的JAXB解组?
对于没有@XmlRootElement注释的类,有没有办法取消marshall?还是我们有义务输入注释 例如:Java 没有XmlRootElement注释的JAXB解组?,java,jaxb,Java,Jaxb,对于没有@XmlRootElement注释的类,有没有办法取消marshall?还是我们有义务输入注释 例如: public class Customer { private String name; private int age; private int id; public String getName() { return name; } @XmlElement public void setName(Stri
public class Customer {
private String name;
private int age;
private int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
让正确注释类的解组代码如下:
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
忽略细节 以下代码用于在没有
@XmlRootElement
public static void main(String[] args) {
try {
StringWriter stringWriter = new StringWriter();
Customer c = new Customer();
c.setAge(1);
c.setName("name");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(new JAXBElement<Customer>( new QName("", "Customer"), Customer.class, null, c), stringWriter);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
InputStream is = new ByteArrayInputStream(stringWriter.toString().getBytes());
JAXBElement<Customer> customer = (JAXBElement<Customer>) jaxbUnmarshaller.unmarshal(new StreamSource(is),Customer.class);
c = customer.getValue();
} catch (JAXBException e) {
e.printStackTrace();
}
}
publicstaticvoidmain(字符串[]args){
试一试{
StringWriter StringWriter=新StringWriter();
客户c=新客户();
c、 设置(1);
c、 设置名称(“名称”);
JAXBContext JAXBContext=JAXBContext.newInstance(Customer.class);
Marshaller=jaxbContext.createMarshaller();
marshaller.marshall(新的JAXBElement(新的QName(“,“Customer”)、Customer.class、null、c)、stringWriter);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
InputStream is=new ByteArrayInputStream(stringWriter.toString().getBytes());
JAXBElement客户=(JAXBElement)jaxbUnmarshaller.unmarshal(新的StreamSource(is),customer.class);
c=customer.getValue();
}捕获(JAXBEException e){
e、 printStackTrace();
}
}
只有在客户类上添加
@xmlacessortype(xmlacesstype.PROPERTY)
或将所有属性设置为私有属性时,上述代码才有效。如果无法将XmlRootElement添加到现有bean中,还可以创建一个holder类,并使用注释将其标记为XmlRootElement。例如:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomerHolder
{
private Customer cusotmer;
public Customer getCusotmer() {
return cusotmer;
}
public void setCusotmer(Customer cusotmer) {
this.cusotmer = cusotmer;
}
}
我使用的通用解决方案如下:
public static <T> String objectToXmlStringNoRoot(T obj, Class<T> objClass, final String localPart) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(objClass);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// To format XML
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//If we DO NOT have JAXB annotated class
JAXBElement<T> jaxbElement = new JAXBElement<>(new QName("", localPart), objClass, obj);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(jaxbElement, sw);
return sw.toString();
}
publicstaticstringobjecttoxmlstringnoroot(tobj,类objClass,最终字符串localPart)抛出jaxbeexception{
JAXBContext JAXBContext=JAXBContext.newInstance(objClass);
Marshaller jaxbMarshaller=jaxbContext.createMarshaller();
//格式化XML的步骤
setProperty(Marshaller.JAXB_格式化的_输出,Boolean.TRUE);
//如果我们没有JAXB注释类
JAXBElement-JAXBElement=newjaxbelement(newqname(“”,localPart),objClass,obj);
StringWriter sw=新的StringWriter();
jaxbMarshaller.marshall(jaxbElement,sw);
返回sw.toString();
}
在另一种情况下,尝试使私有
所有属性,否则无论是否使用XmlRootElement
,JAXB都无法以上述方式工作,您将不需要任何其他注释,除了@xmldattribute
和@xmlmelement
之外,它们描述了在解组之前需要整理的输出?不,不是。这只是一个xml格式良好的示例@查理福莱太棒了!允许我们封送/解封生成的类,而无需在生成后手动将其损坏。阿披实您的解决方案对我有效,但我必须使用@xmlement(name=“Customer”,required=true)注释Customer字段