1

javax.xml.bind.MarshalException - 带有链接异常:[javax.xml.stream.XMLStreamException: Can not output XML declaration, after other output has been done.] at com.sun.xml.bind.v2.runtime.MarshallerImpl .write(MarshallerImpl.java:330) 在 com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:178)

这是我的代码片段:

OutputStream output = new ByteArrayOutputStream();
            XMLOutputFactory xof = XMLOutputFactory.newInstance();
            XMLStreamWriter xsw = xof.createXMLStreamWriter(output);


            QName root = new QName("return");
            JAXBElement<Customer> je = new JAXBElement<Customer>(root, Customer.class, customer);

            xsw.writeStartDocument();
            xsw.setDefaultNamespace("S");
            xsw.writeStartElement("S", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
        xsw.writeStartElement("S", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
        xsw.writeStartElement("ns0", "findCustomerResponse", "http://service.jaxws.blog/");

            context = JAXBContext.newInstance(Customer.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(je, xsw);
            xsw.writeEndDocument();
            xsw.close();
            if(output != null)
            return output.toString();

你有什么想法为什么会发生这种情况?提前致谢

4

1 回答 1

0

您需要在 上指定JAXB_FRAGMENT属性Marshaller以避免此问题。此属性让 JAXB 知道它正在编组到文档的中间,并且它不应该写入标题。

了解更多信息

于 2013-10-21T11:19:56.343 回答