0

我正在将一个 XML 文件(作为 TextMessage)发送到一个队列并在另一个类中接收它。在那里,我想将此文件解组到我的对象(通道)。但是通过这样做,我得到了这个错误:prolog 中不允许内容。

有人可以帮我吗?这是一个学校项目,我真的不知道如何解决这个错误。谢谢!

发件人类

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();
    }
}

接收器类

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();
    }
}
4

0 回答 0