0

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();
    }
}
Warri
  • 217
  • 1
  • 3
  • 14
  • 1
    Please cross check your sending xml file. In XML file very first required thing is xml declaration and if it is not there document node. It seems you have different thing at the start of your XML file. – Navin Rawat Nov 01 '13 at 07:19
  • See also http://stackoverflow.com/questions/5138696/org-xml-sax-saxparseexception-content-is-not-allowed-in-prolog – Raedwald Jul 18 '14 at 09:35

0 Answers0