问答详情
源自:5-4 消息的接收与响应2

我想用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();

    }




提问者:孤_0002 2017-10-13 18:03

个回答

  • 孤_0002
    2017-10-17 15:49:20

    反正它已经生成xml了,把它当成xml进行解析吧。使用dom4j

    ,writer.toString().getRootElement();