如何解组包含@XmlAnyElement和DIFFGR的XML代码 [英] How to unmarshall XML code containing @XmlAnyElement and DIFFGR

查看:90
本文介绍了如何解组包含@XmlAnyElement和DIFFGR的XML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理来自Web服务的soap响应。我使用wsimport工具生成用于处理soap响应的客户端类。下面是示例soap响应和java处理程序。

I am trying to handle soap response from a webservice. I have used wsimport tool to generate client class for handling soap response. Below are the sample soap response and java handler.

肥皂响应:

<?xml version="1.0" encoding="UTF-8"?>
<DataSet xmlns="http://tempuri.org/">
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="NTIS">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Error" type="xs:string" minOccurs="0"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet xmlns="">
      <NTIS diffgr:id="NTIS1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
        <Error>UserName and/or Password are invalid.</Error>
      </NTIS>
    </NewDataSet>
  </diffgr:diffgram>
</DataSet>

Java处理程序类

package org.tempuri;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.w3._2001.xmlschema.Schema;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="GetQueryNewResult" minOccurs="0">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;element ref="{http://www.w3.org/2001/XMLSchema}schema"/>
 *                   &lt;any/>
 *                 &lt;/sequence>
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "getQueryNewResult"
})
@XmlRootElement(name = "GetQueryNewResponse")
public class GetQueryNewResponse {

    @XmlElement(name = "GetQueryNewResult")
    protected GetQueryNewResponse.GetQueryNewResult getQueryNewResult;

    /**
     * Gets the value of the getQueryNewResult property.
     * 
     * @return
     *     possible object is
     *     {@link GetQueryNewResponse.GetQueryNewResult }
     *     
     */
    public GetQueryNewResponse.GetQueryNewResult getGetQueryNewResult() {
        return getQueryNewResult;
    }

    /**
     * Sets the value of the getQueryNewResult property.
     * 
     * @param value
     *     allowed object is
     *     {@link GetQueryNewResponse.GetQueryNewResult }
     *     
     */
    public void setGetQueryNewResult(GetQueryNewResponse.GetQueryNewResult value) {
        this.getQueryNewResult = value;
    }


    /**
     * <p>Java class for anonymous complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType>
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element ref="{http://www.w3.org/2001/XMLSchema}schema"/>
     *         &lt;any/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "schema",
        "any"
    })
    public static class GetQueryNewResult {

        @XmlElement(namespace = "http://www.w3.org/2001/XMLSchema", required = true)
        protected Schema schema;
        @XmlAnyElement(lax = true)
        protected Object any;

        /**
         * Gets the value of the schema property.
         * 
         * @return
         *     possible object is
         *     {@link Schema }
         *     
         */
        public Schema getSchema() {
            return schema;
        }

        /**
         * Sets the value of the schema property.
         * 
         * @param value
         *     allowed object is
         *     {@link Schema }
         *     
         */
        public void setSchema(Schema value) {
            this.schema = value;
        }

        /**
         * Gets the value of the any property.
         * 
         * @return
         *     possible object is
         *     {@link Object }
         *     
         */
        public Object getAny() {
            return any;
        }

        /**
         * Sets the value of the any property.
         * 
         * @param value
         *     allowed object is
         *     {@link Object }
         *     
         */
        public void setAny(Object value) {
            this.any = value;
        }

    }

}

客户端程序:

package org.ritwik.client;

import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.Node;

import org.tempuri.BACOnlineResponse;
import org.tempuri.BACOnlineResponse.BACOnlineResult;
import org.tempuri.BASCOnlineResponse;
import org.tempuri.BASCOnlineResponse.BASCOnlineResult;
import org.tempuri.DEAAppSearch;
import org.tempuri.DEAAppSearchResponse;
import org.tempuri.DEAAppSearchResponse.DEAAppSearchResult;
import org.tempuri.Deawebsvc;
import org.tempuri.DeawebsvcSoap;
import org.tempuri.GetIssueDate;
import org.tempuri.GetIssueDateResponse;
import org.tempuri.GetQueryResponse;
import org.tempuri.GetUserInfoResponse;
import org.tempuri.GetUserInfoResponse.GetUserInfoResult;
import org.tempuri.IsUserValidResponse;
import org.tempuri.ObjectFactory;
import org.w3c.dom.Element;

public class WsClient {

        /**
         * @param args
         * @throws JAXBException 
         */
        public static void main(String[] args)  {
            // TODO Auto-generated method stub

            Deawebsvc deaw=new Deawebsvc();
            DeawebsvcSoap impl=deaw.getDeawebsvcSoap();
            ObjectFactory of=new ObjectFactory();   
            DEAAppSearchResponse dsSearch=of.createDEAAppSearchResponse();
            dsSearch.setDEAAppSearchResult(impl.deaAppSearch("deatest@ritwik.com"," deavalidation", "dea", "bac", "basc", "expirationFrom", "expirationTo", "company", "zip", "state", "pi", "maxRows"));
            System.out.println(dsSearch.getDEAAppSearchResult().toString());
            System.out.println(dsSearch.getDEAAppSearchResult().getAny());



        }

    }

现在的问题是我无法处理来自getDEAAppSearchResult()。getAny()的Result conet。请建议如何处理内容或解组。

Now the problem is I am unable to handle Result conet from "getDEAAppSearchResult().getAny()". Please suggest how to handle the content or unmarshal this.

org.tempuri.DEAAppSearchResponse$DEAAppSearchResult@7bc768
[diffgr:diffgram:null]

org.tempuri.DEAAppSearchResponse$DEAAppSearchResult@7bc768 [diffgr:diffgram: null]

推荐答案

我遇到了类似的问题,我使用Unmarshaller将详细节点解组为有意义的对象。在我的问题中,生成了详细的类(在您的示例中为NewDataSet)。以下内容适合您:

I had a similar problem, I used an Unmarshaller to unmarshal the detailed node to a meaningful object. In my problem, the detailed classes were generated (NewDataSet in your example). The following may work for you:

JAXBContext jaxbContext = JAXBContext.newInstance(NewDataSet.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Node node = (Node) dsSearch.getDEAAppSearchResult().getAny();
NewDataSet dataset = (NewDataSet)unmarshaller.unmarshal(node.getFirstChild());

这篇关于如何解组包含@XmlAnyElement和DIFFGR的XML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆