Java xml serialization null processing

I am using JAXB2 to serialize an object in xml.

Is there a way to force it to create an entire object structure, as in the following example, even if it is not filled with a backup object?

This is my intended result, even if you do not have an asignee property property.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <note> <to xsi:nil="true"/> <from xsi:nil="true"/> <header xsi:nil="true"/> <body>text</body> <assignee> <name xsi:nil="true"/> <surname xsi:nil="true"/> </assignee> </note> 

I use the following code to serialize:

 JAXBContext jc = JAXBContext.newInstance(dataObject.getClass()); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); marshaller.setProperty(Marshaller.JAXB_ENCODING, charset); marshaller.marshal(dataObject, outputStream); 
+4
source share
2 answers

You can do the following:

Note

 import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement @XmlType(propOrder={"to", "from", "header", "body", "assignee"}) public class Note { private String to; private String from; private String header; private String body; private Assignee assignee; @XmlElement(nillable=true) public String getTo() { return to; } public void setTo(String to) { this.to = to; } @XmlElement(nillable=true) public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } @XmlElement(nillable=true) public String getHeader() { return header; } public void setHeader(String header) { this.header = header; } @XmlElement(nillable=true) public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Assignee getAssignee() { return assignee; } public void setAssignee(Assignee assignee) { this.assignee = assignee; } } 

Assignee

We will need to have a means to not, when an unmarshalled Assignee instance should be interpreted as null. I added the isNull () method, which returns true if all fields have a null value.

 import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlJavaTypeAdapter(AssigneeAdapter.class) public class Assignee { private String name; private String surname; @XmlElement(nillable=true) public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement(nillable=true) public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public boolean isNull() { return null == name && null == surname; } } 

Assigneeadapter

AssigneeAdapter uses the Assignee object for the value type and the associated type. This class uses the isNull () method that we added to Assignee:

 import javax.xml.bind.annotation.adapters.XmlAdapter; public class AssigneeAdapter extends XmlAdapter<Assignee, Assignee> { @Override public Assignee unmarshal(Assignee v) throws Exception { if(v.isNull()) { return null; } return v; } @Override public Assignee marshal(Assignee v) throws Exception { if(null == v) { return new Assignee(); } return v; } } 

Demo

 import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Note.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(new Note(), System.out); } } 

See below for more on the XmlAdapter:

+3
source

Source: https://habr.com/ru/post/1332209/


All Articles