java - Jaxb 解码任何类型的 xml 文档

标签 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 部署 Web 服务。但是当我尝试映射 doc 对象并尝试将其作为字符串获取时。我使用 toString 并尝试使用 DOMSource 进行转换。我怎样才能将其转换为字符串。非常感谢任何帮助。

最佳答案

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 参数中使用任何类型进行映射时。它将映射到列表或其他集合,然后我们需要使用 DOM 将其映射到字符串。

关于java - Jaxb 解码任何类型的 xml 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45236269/

相关文章:

java -//格式化java源码注释--------------------------------

java - 使用 JPA 导入数据并保留相同的标识符

c# - 如何使用 WCF 更改外部对象的名称?

java - 解码时出现意外的元素错误

java - 如何为具有多个可调用语句的执行器服务编写 Junit 测试用例

java - 用于瘦客户端部署的 http/AJAX (GWT) 与 Eclipse gui

wcf - 在 .NET 中使用 JSON-RPC Web 服务

php - prestashop 类 'Product' 未找到

java - JAXB 的内存泄漏

java - 什么是 JAXB 生成的 package-info.java