我正在尝试使用Pojo to XML
库转换JAXB
。
我需要最终结果看起来像这样:
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<!--other stuff-->
</soap:Body>
</soap:Envelope>
我尝试了几种不同的方法,但是到目前为止,我还没有成功,这是我的最新尝试。
@XmlRootElement(name = "soap:Envelope")
public class Envelope {
private SoapBody soapBody;
public String toString() {
return "ClassPojo [SoapBody = " + soapBody + "]";
}
public SoapBody getSoapBody() {
return soapBody;
}
@XmlElement(name = "soap:Body")
public void setSoapBody(SoapBody soapBody) {
this.soapBody = soapBody;
}
}
这将转换为以下结果(但缺少
XMLNS
行):<soap:Envelope>
<soap:Body>
<!--Other stuff-->
</soap:Body>
</soap:Envelope>
我尝试将名称空间标签添加到声明中:
@XmlRootElement(name = "soap:Envelope", namespace = "soap")
但这只是使行转换为此
<ns2:soap:Envelope xmlns:ns2="soap">
编辑:
OutputStream os = connection.getOutputStream();
JAXBContext jaxbContext =
JAXBContext.newInstance(MyOtherStuffObject.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(myObject, os);
os.flush();
最佳答案
我尝试将名称空间标签添加到声明中:
@XmlRootElement(name =“ soap:Envelope”,名称空间=“ soap”)
但它只是使线转换为此
您一步一步摆脱了需要...
soap名称空间是http://schemas.xmlsoap.org/soap/envelope/
而不是soap
,那么……如果那样的话会怎样?@XmlRootElement(name = "soap:Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
顺便说一句。但是您真的需要手动创建SOAP Envelope吗?实际上,标准软件包javax.xml.soap
具有与SOAP一起使用的所有功能,您可以将“其他内容”包装到SOAP Envelope中,而不关心自己构建它吗?
更新:
我强烈建议在使用诸如Apache CXF之类的SOAP Web服务时使用常规框架,而不是在较低级别上处理SOAP。
但这可以通过标准JDK类来完成。
示例代码:
package com.foo.tests;
import java.io.ByteArrayOutputStream;
import java.util.Calendar;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Document;
public class TestSOAPMessage {
static MessageFactory factory;
static DocumentBuilderFactory documentFactory;
static JAXBContext jaxbCtx;
static com.foo.tests.pojo.ObjectFactory myStuffFactory = new com.foo.tests.pojo.ObjectFactory();
static {
try {
factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
documentFactory = DocumentBuilderFactory.newInstance();
jaxbCtx = JAXBContext.newInstance(com.foo.tests.pojo.MyStuffPojo.class);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String... args) {
try {
// prepare test MyStuff JAXB POJO
com.foo.tests.pojo.MyStuffPojo myStuff = myStuffFactory.createMyStuffPojo();
// populate myStuff Pojo
myStuff.setMyPropertyA("property A");
myStuff.setTimestamp(Calendar.getInstance());
myStuff.setMessageId(UUID.randomUUID().toString());
//---
// marshal JAXB Pojo to DOM Document
Document myStuffDoc = documentFactory.newDocumentBuilder().newDocument();
//*** myStuff has @XmlRootElement annotation
jaxbCtx.createMarshaller().marshal(myStuff, myStuffDoc);
//*** myStuff does not have @XmlRootElement annotation wrap it and use JAXBElement instead
// JAXBElement<com.foo.tests.pojo.MyStuffPojo myStuff> jaxbWrapper = myStuffFactory.createMyStuffPojo(myStuff);
// jaxbCtx.createMarshaller().marshal(jaxbWrapper, myStuffDoc);
//marshal JAXB Pojo to DOM Document
Document myStuffDoc = documentFactory.newDocumentBuilder().newDocument();
jaxbCtx.createMarshaller().marshal(jaxbWrapper, myStuffDoc);
//Create SOAPMessage
SOAPMessage myMessage = factory.createMessage();
//Optional if we'd like to set those properties...
myMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
myMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
// set myStuff into SOAPBody
myMessage.getSOAPBody().addDocument(myStuffDoc);
//All done. Save changes
myMessage.saveChanges();
// Just for test: print message
ByteArrayOutputStream finalBos = new ByteArrayOutputStream();
myMessage.writeTo(finalBos);
System.out.println("my Message: \r\n" + new String(finalBos.toByteArray()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
关于java - 使用JAXB从XML创建POJO,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46430175/