public static String textMessageToXml(TextMessage textMessage) throws JAXBException{
JAXBContext jaxbContext = JAXBContext.newInstance(TextMessage.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
StringWriter writer = new StringWriter();
jaxbMarshaller.marshal(textMessage, writer);
return writer.toString();
}
反正它已经生成xml了,把它当成xml进行解析吧。使用dom4j
,writer.toString().getRootElement();