我試圖使用JAXB
庫轉換Pojo to XML
。使用JAXB從XML創建POJO
我需要的最終結果是這個樣子:
<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();
soap命名空間是否必須是顯式的?對於大多數解析器,應該等同於。 –