解组时出现意外的元素错误

解组时出现意外的元素错误

本文介绍了解组时出现意外的元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该将简单的xml解组为belo但是会收到错误

I should unmarshall simple xml as belo but getting error as

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

This is the xml present in response.xml file

<?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类

The webresponse class for storing the retrieved xml

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类:

Tester类

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());

    }

}

package-info.java:

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



package com.example;

import javax.xml.bind.annotation.*;

我使用了本网站提供的解决方案但无法解决请帮助

i used the solutions present in this site but could not solve please help

推荐答案

第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)

问题是XML中指定的命名空间( response / v1 request / v1 )。

The problem is the namespaces specified in the XML (response/v1 and 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内容不匹配。解决这个问题的一种方法是引入一个状态类。

Your current object model and mappings do not match the XML content. One way to solve this would be to introduce a Status class as answered by Ash.

状态

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;
    }

}

这篇关于解组时出现意外的元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:49