Jaxb解组xml文档anytype

时间:2017-07-21 11:15:40

标签: java web-services jaxb cxf marshalling

我有一个服务方法,其方法有2个参数:

    public int ReadXmlDocAsString(@WebParam(name = "password") String password, 
                                  @WebParam(name = "doc") com.vincari.hl7.jaxws.xmlDoc<Object> doc){

    }

我的xmlDoc类如下:

public class xmlDoc<T> {
@XmlMixed
@XmlAnyElement(lax = true)
protected List<T> content;
public List<T> getContent() {
    if (content == null) {
        content = new ArrayList<T>();
    }
    return this.content;
}
public void setContent() {
    content = this.content;
}   
}

我的样本肥皂请求是:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sample="http://sample.vincari.com/">
<soap:Header/>
<soap:Body>
      <sample:ReadXmlDocAsString>
         <!--Optional:-->
         <password>1234</password>
         <!--Zero or more repetitions:-->
         <doc>
         <Header Application="SourceApp">
         <CustomerData>
         <Customer AccountNumber="1234" customername="ABCD">
         </Customer>
         <CustomerData>
         </Header>
         </doc>
      </sample:InsertPatientInfoImpl>
   </soap:Body>
</soap:Envelope>

我可以使用cxf部署webservice。但是当我尝试映射doc对象并尝试将其作为字符串时。我曾经使用过String并试图使用DOMSource进行转换。如何将其转换为字符串。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:1)

Document inputDoc = ((Element) doc.getContent().get(0)).getOwnerDocument();
String inputPayload = "";
StringWriter sw = new StringWriter();
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(new DOMSource(inputDoc), new StreamResult(sw));
            inputPayload = sw.toString();
            System.out.println("Read the input stream successfully "+ inputPayload);

使用wsdl参数中的anytype进行映射时。它将映射到列表或其他集合,然后我们需要使用DOM将其映射到字符串。