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();
你有什么想法为什么会发生这种情况?提前致谢