I'm sending a XML file (as a TextMessage) to a queue and receive it in another class. There I want to unmarshall this file to my object (Passage). But by doing this I'm getting this error: Content is not allowed in prolog.
Can somebody help me with this? It's for a school project and I really don't know how to fix this error. Thanks!
Sender class
public class SenderPassage {
public static void main(String[] args) throws JMSException, JAXBException, IOException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("PASSAGE");
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
JAXBContext context = JAXBContext.newInstance(Passage.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Passage passage = new Passage().RandomPassage();
System.out.println(passage.toString());
System.out.println("Marshalling...");
StringWriter writer = new StringWriter();
marshaller.marshal(passage, writer);
System.out.println(writer.toString());
String xml = writer.toString();
TextMessage message = session.createTextMessage(xml);
System.out.println(message);
producer.send(message);
producer.close();
session.close();
connection.close();
}
}
Receiver class
public class ReceiverPassage {
public static void main(String[] args) throws JMSException, MarshalException, ValidationException, FileNotFoundException, JAXBException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("PASSAGE");
MessageConsumer consumer = session.createConsumer(destination);
JAXBContext context = JAXBContext.newInstance(Passage.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
TextMessage message = (TextMessage) consumer.receive();
//String m = message.toString();
//m = m.toString().trim().replaceFirst("^([\\W]+)<","<");
//System.out.println(m);
Passage passage = (Passage) unmarshaller.unmarshal(new StringReader(message.toString()));
System.out.println(passage.toString());
//Passage passage = (Passage) Unmarshaller.unmarshal(Passage.class, message);
//Passage passage = (Passage) Unmarshaller.unmarshal(new StringReader(StringWriter));
//TextMessage textMessage = (TextMessage) consumer.receive();
//System.out.println(passage.toString());
consumer.close();
session.close();
connection.close();
}
}