-1

WebSphere で実行される Java Web サービス クライアントを作成しています。RAD Developer で新しい「Web サービス クライアント」プロジェクトを作成し、WSDL を指定して「トップダウン Java Bean」を指定すると、多数のファイルが自動生成されました。

操作の 1 つが「GetAddressData」です。RAD Developer は、「GetAddressData.java」と「GetAddressDataResonse.java」を自動生成し、どちらも「XmlRootElement」で注釈を付けました。

「GetAddressData」の引数の 1 つは「RequestData」です。これは、worklow、module、username、および id の 4 つの文字列で構成される単純なオブジェクトです。RAD Developer は、私のために「RequestData.java」も生成しました。

Q: 一度に 1 フィールドずつレコードをパックおよびアンパックする代わりに、生の XML を JAXB の「RequestData」オブジェクトに置き換える方法はありますか?

私はこのようなことを試しました:

private static String theXml =   
    "<requestOptions>\n" +  
    "  <WorkflowName>unmarshalTestWorkflow</WorkflowName>\n" +  
    "  <ModuleName>unmarshalTestModule</ModuleName>\n" +  
    "  <UserName>unmarshalTestName</UserName>\n" +  
    "  <TransactionId>0099</TransactionId>\n" +  
    "</requestOptions>\n";  

private RequestOptions mkRequestOptions () throws Exception {  
    JAXBContext context = JAXBContext.newInstance(RequestOptions.class);  
    Unmarshaller unmarshaller = context.createUnmarshaller();  
    Object obj = unmarshaller.unmarshal(new StringReader (theXml));  
    RequestOptions requestOptions = (RequestOptions)obj;
    ...

しかし、私は取得し続けます:

error: Unexpected element "requestOptions". Expected elements are "".

どんな助けでも大歓迎です!前もって感謝します。

4

1 に答える 1

4

2 つのことを行う必要があります。

  1. ルートの xmlns: <requestOptions xmlns=\"http://www.company.com/ns\">. これは XSD に戻ります。
  2. RequestData は @XmlRootElement ではないように聞こえるので、JAXBElement でラップして非整列化する必要があります。

ここに示されています:

public class Test
{
  static String randomXml =
      "<divisionRequestHeader xmlns=\"http://www.company.com/ns/\">"
        + "<id>fake id</id>"
        + "<CoName>My Co Name</CoName>" + "<User>"
        + "<Type>EXTERNAL</Type>" + "<Value>me</Value>" + "</User>"
        + "<Count>100</Count>"
        + "<Requestor>My App Requesting</Requestor>"
        + "</divisionRequestHeader>";

  public static void main(String[] args) throws Exception
  {
    JAXBContext context = JAXBContext.newInstance(DivisionRequestHeader.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Source source = new StreamSource(new StringReader(randomXml));

    JAXBElement<DivisionRequestHeader> jaxbElement = unmarshaller.unmarshal(source,
            DivisionRequestHeader.class);
    DivisionRequestHeader header = jaxbElement.getValue();

    System.out.println(header.toString());
  }
}

jaxb toString プラグインを使用した出力:

com.company.ns.DivisionRequestHeader@620c620c[id=fake id, coName=My Co Name,
    user=com.company.ns.User@79e479e4[type=EXTERNAL, value=me], count=100,
    requestor=My App Requesting]
于 2013-02-06T03:41:35.420 に答える