4

シンプルなxmlをベロとしてアンマーシャリングする必要がありますが、次のようにエラーが発生します

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com/service/response/v1", local:"WebResponse"). Expected elements are <{http://example.com/service/request/v1}WebResponse>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)

I tried the solutions in this site but could not solve pls help.

これは、response.xml ファイルに存在する xml です。

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <WebResponse xmlns="http://example.com/service/response/v1">
- <Status>
  <StatusCode>0</StatusCode> 
  <Description>Transaction was successful</Description> 
  </Status>
  </WebResponse>

以下は私のコードです:

WebResponse クラス:

取得した xml を格納するための webresponse クラス

import javax.xml.bind.annotation.*;

@XmlRootElement(name="WebResponse")
public class WebResponse {

    private long statusCode;
    private String description;
    @XmlElement(name= "StatusCode")
    public long getStatusCode() {
        return statusCode;
    }
    public void setStatusCode(long statusCode) {
        this.statusCode = statusCode;
    }
    @XmlElement(name= "Description")
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

WebResponseMain クラス:

テスタークラス

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;

import java.io.FileReader;
import com.example.WebResponse;


public class WebResponseMain {

    public static void main(String[] args) throws Exception{
        JAXBContext context = JAXBContext.newInstance(WebResponse.class);
        Unmarshaller um = context.createUnmarshaller();
        WebResponse WR = (WebResponse) um.unmarshal(new FileReader("c:/tem/Response.XML"));
        System.out.println("StatusCode: " + WR.getStatusCode() + " Description "
                 +WR.getDescription());

    }

}

パッケージ情報.java:

@XmlSchema(namespace = "http://example.com/service/request/v1",elementFormDefault=XmlNsForm.QUALIFIED)



package com.example;

import javax.xml.bind.annotation.*;

このサイトにあるソリューションを使用しましたが、解決できませんでした 助けてください

4

2 に答える 2

4

パート1

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com/service/response/v1", local:"WebResponse"). Expected elements are <{http://example.com/service/request/v1}WebResponse>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)

response/v1問題は、XML (および)で指定された名前空間request/v1です。

<WebResponse xmlns="http://example.com/service/response/v1">

package-infoは異なります

@XmlSchema(namespace = "http://example.com/service/request/v1", elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;

パート2

現在のオブジェクト モデルとマッピングが XML コンテンツと一致しません。これを解決する 1 つの方法は、Ash が回答しStatusたクラスを導入することです。

状態

import javax.xml.bind.annotation.XmlElement;

public class Status {

    private long statusCode;
    private String description;

    @XmlElement(name = "StatusCode")
    public long getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(long statusCode) {
        this.statusCode = statusCode;
    }

    @XmlElement(name = "Description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

WebResponse

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "WebResponse")
public class WebResponse {

    private Status status;

    @XmlElement(name="Status")
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

}
于 2013-01-25T10:47:49.577 に答える
1

Statusクラスを追加して、 とWebResponseフィールドの間に配置してみてください。

XML 要素は WebResponse -> Status -> StatusCode/Description に移動するため、クラスには 1 つのフィールドのみが必要であると思いますWebResponse: type の「ステータス」Status。クラスStatusにはフィールドStatusCodeDescriptionフィールドが必要です。

編集:また、 @XmlElement 注釈はメソッドではなくフィールドにあると思いましたが、それを行ってからしばらく経ちました...

于 2013-01-25T10:39:02.593 に答える