我想用jaxb生成xml文件但是總有<?xml version="1.0" encoding="utf-8">這個文檔聲明,有什么方法可以去掉這個聲明
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();
?? ?}
2017-10-17
反正它已經生成xml了,把它當成xml進行解析吧。使用dom4j
,writer.toString().getRootElement();