【问题标题】:Jaxb Unmarshalling an xml document anytypeJaxb Unmarshalling an x​​ml document anytype
【发布时间】:2017-07-21 11:15:39
【问题描述】:

我有一个服务方法,它有一个带有 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 部署 web 服务。但是当我尝试映射 doc 对象并尝试将其作为字符串获取时。我使用 toString 并尝试使用 DOMSource 进行转换。如何将其转换为字符串。非常感谢任何帮助。

【问题讨论】:

    标签: java web-services jaxb cxf marshalling


    【解决方案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 将其映射到字符串。

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2020-10-11
      相关资源
      最近更新 更多