I am trying to perform JAXB Unmarshalling for a large XML file so using the XMLEventReader but it's not working as expected and not performing the Unmarshalling.
Following is the XML file that I am trying to unmarshal:
<Customers>
<Customer>
<name>BATMAN</name>
<age>2008</age>
</Customer>
<Customer>
<name>SuperMan</name>
<age>2022</age>
</Customer>
</Customers>
Following is the Customer.class which will be used for unmarshalling:
#XmlRootElement(name = "Customer")
#XmlType(name = "Customer", propOrder = {"name", "age"})
#XmlAccessorType(XmlAccessType.FIELD)
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#ToString
public class Customer {
private String name;
private String age;
}
Following is the Main class which will unmarshal each of the customer and store in the customer field but it returns null. Normally when I would expect it to print me the customer information.
class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException, JsonProcessingException {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("Customer.xml");
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final XMLEventReader streamReader = xmlInputFactory.createXMLEventReader(inputStream);
final JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
while (streamReader.hasNext()) {
final XMLEvent e = streamReader.nextEvent();
if (e.isStartElement() && ((StartElement) e).getName().getLocalPart().equals("Customer")) {
System.out.println(((StartElement) e).getName().getLocalPart());
final Customer customer = unmarshaller.unmarshal(streamReader, Customer.class).getValue();
System.out.println(customer);
}
}
}
}
I tried with the XMLStreamReader and it works perfectly. But I want to make use of XMLEventReader peak() functionality. Hence, I am trying to make use of XMLEventReader.
I looked at many examples and they provided similar examples not sure what's going wrong in my case.
Can someone please help me in understanding the issue?
After trying few things I was able to get it working. Posting the answer here so it can be useful to someone in the future:
class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException, JsonProcessingException {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("Customer.xml");
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputStream);
final JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
while (xmlEventReader.peek() != null) {
XMLEvent event = xmlEventReader.peek();
if (event.isStartElement() && ((StartElement) event).getName().getLocalPart().equals("Customer")) {
Customer customer = unmarshaller.unmarshal(xmlEventReader, Customer.class).getValue();
System.out.println("Normal Customer : " + customer);
} else {
xmlEventReader.next();
}
}
}
}
Related
I want to convert SOAP XML to Java Object without using xsd files and other stuff. I generate SOAP XML using POJO classes, #Xml annotations. What I want is to somehow deserialize String to not Envelope.class Object, but NumberToWords.class Object.
And by the way I didn't wanna have ns2:/xmlns="" stuff, that's why I used final static String as #XmlAttribute`s instead of namespace initializations.
Here are my classes >>>
#Setter
#Builder
#ToString
#NoArgsConstructor
#AllArgsConstructor
#XmlRootElement(name = "soap:Envelope")
public class Envelope {
#XmlAttribute(
name = "xmlns:soap"
)
#JacksonXmlProperty(isAttribute = true)
private static final String xmlns = "http://schemas.xmlsoap.org/soap/envelope/";
#XmlElement(
name = "soap:Body"
)
private Body body;
public String printBody() {
return body.toString();
}
}
#Getter
#Setter
#Builder
#NoArgsConstructor
#AllArgsConstructor
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Body {
#XmlElement(name = "NumberToWords")
private NumberToWords numberToWords;
#XmlElement(name = "Name")
private Name name;
}
#Getter
#Builder
#ToString
#NoArgsConstructor
#AllArgsConstructor
#XmlAccessorType(XmlAccessType.FIELD)
public class NumberToWords {
#XmlAttribute(name = "xmlns")
private final static String xmlns = "http://www.dataaccess.com/webservicesserver/";
#XmlElement(name = "ubiNum")
private Integer ubiNum;
}
Mapper Class which gets Marshaller Bean from another config class, but I decided to not add it here.
#Component
#RequiredArgsConstructor
public class EnvelopeMapper {
private final Marshaller marshaller;
#SneakyThrows
public String getXml(NumberToWords numberToWords) {
Envelope envelope = Envelope.builder()
.body(
Body.builder()
.numberToWords(numberToWords)
.build())
.build();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(envelope, stringWriter);
return stringWriter.toString();
}
}
localhost:8080/get-num-xml/123
STRING:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
<ubiNum>123</ubiNum>
</NumberToWords>
</soap:Body>
</soap:Envelope>
When I call this endpoint it throws an exception.
#GetMapping("/get-num-xml/{num}")
public String getNumXml(#PathVariable Integer num) throws JAXBException {
String xml = envelopeService.getNumberToWords(num);
NumberToWords numberToWords = (NumberToWords) unmarshaller.unmarshal(new StringReader(xml));
System.out.println(numberToWords.getUbiNum());
return envelopeService.getNumberToWords(num);
}
GIVES:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{}Name>,<{}body>,<{}soap:Envelope>
This one is also throws an exception.
#GetMapping("/get-num-xml/{num}")
public String getNumXml(#PathVariable Integer num) throws JAXBException, SOAPException, IOException {
String xml = envelopeService.getNumberToWords(num);
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(xml.getBytes()));
JAXBContext jc = JAXBContext.newInstance(NumberToWords.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
NumberToWords rc = (NumberToWords) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());
return envelopeService.getNumberToWords(num);
}
GIVES:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.dataaccess.com/webservicesserver/", local:"NumberToWords"). Expected elements are (none)
I don't have a clue how to unmarshal xml, so I hope someone has an answer for this one.
I'm using spring batch to generate xml files.
My writer looks like :
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Person.class);
StaxEventItemWriter<Person> itemWriter = new StaxEventItemWriter<>();
itemWriter.setRootTagName("Persons");
itemWriter.setMarshaller(marshaller);
itemWriter.setRootElementAttributes(new HashMap<String, String>() {{
put("xmlns", "http://entreprise.uk/ns");
}});
itemWriter.setResource(new FileSystemResource(Paths.get("personOutput.xml").toFile()));
itemWriter.afterPropertiesSet();
return itemWriter;
And the person class :
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Person")
public class Person {
//...
}
When I run the batch I get this error :
Caused by: javax.xml.stream.XMLStreamException: xmlns has been already bound to . Rebinding it to http://entreprise.uk/ns is an error
Anyone knows how to fix it ? I need to see xmlns attribut at the root element like :
<?xml version="1.0" encoding="UTF-8"?>
<Persons xmlns="http://entreprise.uk/ns">
<person>...</person>
</Persons>
I'm using spring-boot-starter-batch:2.3.5.RELEASE
To add namespace at the root level, you have to modify the rootTagName in your configuration.
rootTagName("{http://entreprise.uk/ns}Persons")
Hope this solves your problem.
I would personally go with a container class called Persons that has an array/list of Person.
#XmlRootElement(name = "Persons", namespace = "http://entreprise.uk/ns")
#XmlAccessorType(XmlAccessType.FIELD)
public class Persons {
#XmlElement(name = "Person")
private List<Person> persons;
//...
}
This should also remove the need for some of the configurations you currently have to set.
There are 2 ways to solve this issue:
Method-1
Using the package-info file:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Persons xmlns="http://entreprise.uk/ns">
<person>
<firstName>Batman</firstName>
</person>
</Persons>
package-info.java:
#XmlSchema(
elementFormDefault = XmlNsForm.QUALIFIED,
namespace = "http://entreprise.uk/ns",
xmlns = {#XmlNs(prefix = "", namespaceURI = "http://entreprise.uk/ns")})
package stackover;
import jakarta.xml.bind.annotation.XmlNs;
import jakarta.xml.bind.annotation.XmlNsForm;
import jakarta.xml.bind.annotation.XmlSchema;
Persons:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Persons")
#Data
public class Persons {
private Person person;
}
Person:
#Data
#XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private String firstName;
}
Main:
public class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("students.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Persons.class).createUnmarshaller();
final Persons persons = unmarshaller.unmarshal(xmlStreamReader, Persons.class).getValue();
System.out.println(persons.toString());
Marshaller marshaller = JAXBContext.newInstance(Persons.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(persons, System.out);
}
}
Output:
Persons(person=Person(firstName=Batman))
<Persons xmlns="http://entreprise.uk/ns">
<person>
<firstName>Batman</firstName>
</person>
</Persons>
Method-2
Using the prefix with namespace URI for XML and adding to the root class:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Persons xmlns:ns0="http://entreprise.uk/ns">
<person>
<firstName>Batman</firstName>
</person>
</Persons>
Persons:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Persons", namespace = "http://entreprise.uk/ns")
#Data
public class Persons {
private Person person;
}
Person:
#Data
#XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private String firstName;
}
Main:
public class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("students.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Persons.class).createUnmarshaller();
final Persons persons = unmarshaller.unmarshal(xmlStreamReader, Persons.class).getValue();
System.out.println(persons.toString());
Marshaller marshaller = JAXBContext.newInstance(Persons.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(persons, System.out);
}
}
Output:
Persons(person=Person(firstName=Batman))
<ns0:Persons xmlns:ns0="http://entreprise.uk/ns">
<person>
<firstName>Batman</firstName>
</person>
</ns0:Persons>
TL;DR: When I unmarshall from XML to POJO I only have the XmlAttributes well mapped, however all XmlElement are null.
Hi there!
I have the following problem. This class was generated with JAXB
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"activity"
})
#XmlRootElement(name = "activityDetails", namespace = "http://lorem.ipsum.com/")
public class ActivityDetails {
#XmlElement(required = true)
protected Activity activity;
#XmlAttribute(name = "schemaVersion", required = true)
protected float schemaVersion;
#XmlAttribute(name = "actionType")
protected ActionTypes actionType;
#XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar timestamp;
This is an example XML
<activityDetails
actionType="CREATE"
schemaVersion="2.0"
timestamp="2020-01-02T15:31:50.549Z"
xmlns="http://lorem.ipsum.com/">
<activity>
<activityId>
<start>2020-01-01T03:00:00Z</start>
<end>2020-01-02T02:59:00Z</end>
</activityId>
</activity>
</activityDetails>
But, when this code is executed (please don't judge me, it's legacy code):
Object xmlClass = Class.forName("com.lorem.ipsum." + className).getConstructor().newInstance();
final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass.getClass());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object object = unmarshaller.unmarshal(new StringReader(element));
The result "object" have all XmlAttribute well mapped, but no one of their XmlElement
PS: The namespace in the generated class was added manually, if I don't do that I have this exception:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://lorem.ipsum.com/", local:"activityDetails"). Expected elements are <{}activityDetails>
Thanks in advance.
UPDATED: If I set all #XmlElement the namespace property I finally map the elements, but I must intervene all the classes. Is there another way to achieve that without having to modify all the fields of all the classes?
I guess I am able to figure out the issue. This is happening because you have not provided any prefix to your namespace in XML. Following code would work for your provided sample XML:
XML:
<activityDetails
actionType="CREATE"
schemaVersion="2.0"
timestamp="2020-01-02T15:31:50.549Z"
xmlns:ns0="http://lorem.ipsum.com/">
<activity>
<activityId>
<start>2020-01-01T03:00:00Z</start>
<end>2020-01-02T02:59:00Z</end>
</activityId>
</activity>
</activityDetails>
ActivityDetails.class:
#XmlAccessorType(XmlAccessType.FIELD)
#Data
#XmlRootElement(name = "activityDetails", namespace = "http://lorem.ipsum.com/")
public class ActivityDetails {
private Activity activity;
#XmlAttribute
private float schemaVersion;
#XmlAttribute
private String actionType;
#XmlAttribute
private String timestamp;
}
Activity.class:
#Data
#XmlAccessorType(XmlAccessType.FIELD)
public class Activity {
private ActivityID activityId;
}
ActivityID.class:
#Data
#XmlAccessorType(XmlAccessType.FIELD)
public class ActivityID {
private String start;
private String end;
}
Main.class:
public class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("activity.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(ActivityDetails.class).createUnmarshaller();
final ActivityDetails activityDetails = unmarshaller.unmarshal(xmlStreamReader, ActivityDetails.class).getValue();
System.out.println(activityDetails.toString());
Marshaller marshaller = JAXBContext.newInstance(ActivityDetails.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(activityDetails, System.out);
}
}
Following is your output:
ActivityDetails(activity=Activity(activityId=ActivityID(start=2020-01-01T03:00:00Z, end=2020-01-02T02:59:00Z)), schemaVersion=2.0, actionType=CREATE, timestamp=2020-01-02T15:31:50.549Z)
<ns0:activityDetails xmlns:ns0="http://lorem.ipsum.com/" schemaVersion="2.0" actionType="CREATE" timestamp="2020-01-02T15:31:50.549Z">
<activity>
<activityId>
<start>2020-01-01T03:00:00Z</start>
<end>2020-01-02T02:59:00Z</end>
</activityId>
</activity>
</ns0:activityDetails>
Finally I found this solution: Instead of putting the namespace in each of the XMLElements I put the following package-info.java in the package of the generated classes.
#XmlSchema(
elementFormDefault=XmlNsForm.QUALIFIED,
namespace="http://lorem.ipsum.com/",
xmlns={#XmlNs(prefix="", namespaceURI="http://lorem.ipsum.com/")})
package com.lorem.ipsum.generated;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
How can I to process such structure with the help of JAXB?
There is one parent root catalog and many other in it, that are keeping info about many objects (states, for example).
<?xml version='1.0' encoding='utf-8'?>
<root>
<states>
<state>
<name>Alabama</name>
<id>al</id>
</state>
<state>
<name>Alaska</name>
<id>ak</id>
</state>
...
</states>
<airports>
<airport>
...
</airport>
<airport>
...
</airport>
...
</airports>
...
</root>
In case states, I created class States:
#XmlRootElement(name = "root")
#XmlAccessorType(XmlAccessType.FIELD)
public class States {
#XmlElement(name="states")
private List<State> states;
public List<State> getBrokers(){
return states;
}
public void setBrokers(List<State> states) {
this.states = states;
}
}
and class State:
#XmlAccessorType(XmlAccessType.FIELD)
public class State {
#XmlElement(name = "name")
private String name;
#XmlElement(name = "id")
private String id;
public State(){}
public State(String id, String name) {
this.id = id;
this.name = name;
}
But when I call the States object
public class JaxbParser {
public Object getObject(File file, Class c) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object object = unmarshaller.unmarshal(file);
return object;
}
public void saveObject(File file, Object o) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(o.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(o,file);
}
}
public static void main(String[] args) throws JAXBException {
JaxbParser parser = new JaxbParser();
States brol = (States) parser.getObject(new File("C:\\Users\\...\\1.xml"), States.class);
List<State> brokerList = brol.getStates();
System.out.println(brol);
}
the stateList contains only one state with id and name equals null.
What am I doing wrong ?
public static void main(String args[]) throws
JAXBException {
try {
File file = new File("....xml");
JAXBContext jaxbContext =
JAXBContext.newInstance(Classname.class);
Unmarshaller jaxbUnmarshaller =
jaxbContext.createUnmarshaller();
Classname objectName = (Classname)
jaxbUnmarshaller.unmarshal(file);
List<Classname> list = objectName.getStates();
int i = 1;
for (State ans : list){
.....
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
We want to implement a RESTful-Webservice with JAX-RS and JAXB. We have a PUT method that consumes xml, that looks like the following:
<mailAccount>
<id>-1</id>
<name>test</name>
<mailaddress>test#gmx.de</mailaddress>
<password>1234</password>
<servertype>IMAP</servertype>
<host>hallo</host>
<port>5678</port>
<encryption>SSL/TLS</encryption>
<authentication>true</authentication>
<interval>12</interval>
</mailAccount>
We also have a MailAccount.class that is mapped to the xml.
#XmlRootElement
public class MailAccount {
private String name;
private String mailaddress;
private String password;
private String servertype;
private String host;
private int port;
private String encryption;
private boolean authentication;
private int interval;
private int id;
getter + setter...
}
The PUT-Method looks like the following:
#PUT()
#Path("/addMailAccount")
#Consumes(MediaType.APPLICATION_XML)
#Produces(MediaType.TEXT_HTML)
public Response addMailAccount(JAXBElement<MailAccount> mail) throws Exception{
MailAccount mailAccounts = mail.getValue();
StringWriter sw = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(MailAccount.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(mailAccounts, sw);
String xmlConsume = sw.toString();
Source source = new StreamSource(new StringReader(xmlConsume));
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(MailAccountService.class.getResource("/emailAddresses.xsd"));
Validator validator = schema.newValidator();
//validator.validate(source);
return Response.status(200).entity(xmlConsume +"..."+ mailAccounts.getMailadress()).build();
}
Our goal is to marshalling the JAXB-Element to validate it against an XML-Schema. But the problem is the marshalling: First of all the elements are not in the correct order. The use of the propOrder Tag results every time in an internal server error.
The second problem is that the element "mailaddress" is empty. It is not marshalling and when i put it into the Response of this method, the value is null.
Here is what the PUT-Method is returning:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mailAccount>
<authentication>true</authentication>
<encryption>SSL/TLS</encryption>
<host>hallo</host>
<id>-1</id>
<interval>12</interval>
<name>test</name>
<password>1234</password>
<port>5678</port>
<servertype>IMAP</servertype>
</mailAccount>
...null
Here are a couple items that should help:
propOrder
Below is an example of applying a propOrder to your class. It is important to remember that you need to include all mapped fields/properties that are mapped to XML elements in the propOrder.
import javax.xml.bind.annotation.*;
#XmlRootElement
#XmlType(propOrder={"id", "name", "mailaddress", "password", "servertype", "host", "port", "encryption", "authentication", "interval"})
#XmlAccessorType(XmlAccessType.FIELD)
public class MailAccount {
private String name;
private String mailaddress;
private String password;
private String servertype;
private String host;
private int port;
private String encryption;
private boolean authentication;
private int interval;
private int id;
}
Doing Schema Validation as Part of the Marshal Operation
Below is a standalone example that demonstrates how you can leverage schema validation as part of the marshal operation instead of doing it as a separate operation (see: http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html).
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.validation.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MailAccount.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum20699078/input.xml");
MailAccount mailAccount = (MailAccount) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(MailAccountService.class.getResource("/emailAddresses.xsd"));
marshaller.setSchema(schema);
marshaller.marshal(mailAccount, System.out);
}
}
For all those, who have to implement the XML validation with JAX-RS webservice, this is my PUT-Method after the hints from above. It works now fine for me.
#PUT()
#Path("/addMailAccount")
#Consumes(MediaType.APPLICATION_XML)
#Produces(MediaType.TEXT_HTML)
public Response addMailAccount(JAXBElement<MailAccount> mail) throws MailAccountServiceException{
String xmlConsume = "";
try{
MailAccount mailAccounts = mail.getValue();
StringWriter sw = new StringWriter();
JAXBContext jaxbContext = JAXBContext.newInstance(MailAccount.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(MailAccountService.class.getResource("/mailAccount.xsd"));
jaxbMarshaller.setSchema(schema);
jaxbMarshaller.marshal(mailAccounts, sw);
xmlConsume = sw.toString();
}
catch(Exception e){
throw new MailAccountServiceException("Fehlerhafte Anfrage: " + e.getCause().getMessage());
}
return Response.status(200).entity(xmlConsume).build();
}