Nota: Sono il lead EclipseLink JAXB (MOXy) e un membro dell'esperto JAXB (JSR-222) group,
Segue come è possibile farlo se si utilizza MOXy come provider JAXB.
JAVA MODELLO
clienti
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement(namespace="http://www.example.com")
@XmlType(namespace="http://www.example.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlAttribute
private int id;
@XmlElement(namespace="http://www.example.com")
private String firstName;
@XmlElement(namespace="http://www.example.com", nillable=true)
private String lastName;
@XmlElement(namespace="http://www.example.com")
private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
}
PhoneNumber
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
@XmlAttribute
private String type;
@XmlValue
private String number;
}
jaxb.properties
Per specificare moxy come provider JAXB è necessario includere un file chiamato jaxb.properties
nello stesso pacchetto come il modello di dominio con la seguente voce (vedi: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
CODICE DEMO
i nput.xml
<?xml version="1.0" encoding="UTF-8"?>
<ns0:customer xmlns:ns0="http://www.example.com" id="123">
<ns0:firstName>Jane</ns0:firstName>
<ns0:lastName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<ns0:phoneNumbers type="work">555-1111</ns0:phoneNumbers>
</ns0:customer>
Demo
Nel codice demo di seguito useremo gli stessi metadati JAXB per convertire un documento XML in oggetti Java e quindi convertire gli oggetti di nuovo a JSON. Con MOXy è possibile specificare l'output JSON impostando una proprietà su Marshaller
.
import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum15357366/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml)
;
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
marshaller.marshal(customer, System.out);
}
}
JSON uscita
Sotto è l'uscita JSON. Nota come non ci sono indicatori corrispondenti a namespace o attributi XML. Si noti inoltre che la raccolta della dimensione uno è stata correttamente rappresentata come una matrice JSON (un problema con altri approcci).
{
"id" : 123,
"firstName" : "Jane",
"lastName" : null,
"phoneNumbers" : [ {
"type" : "work",
"value" : "555-1111"
} ]
}
@BDoughan: Sì, l'ho visto anche nel tuo blog. Ma sto ottenendo ClassNotFound Exception su JAXBContextFactory.Io ho incluso jaxb.properties e ho inserito anche una dipendenza in pom.xml. cortesemente guidami per la corretta dipendenza per quella classe – Arun
@ user1933756 - Ecco un 'pom.xml' da un esempio che ho ospitato su GitHub che puoi usare come guida: https://github.com/bdoughan/blog20110819 /blob/master/pom.xml –
Grazie mille Doughan :) funziona alla grande !!! – Arun