Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:02:49+00:00 2026-06-17T22:02:49+00:00

I should unmarshall simple xml as belo but getting error as Exception in thread

  • 0

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.

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>

Below is my code :

WebResponse class:

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

Tester class

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:02:50+00:00Added an answer on June 17, 2026 at 10:02 pm

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

    The problem is the namespaces specified in the XML (response/v1 and request/v1).

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

    and package-info are different

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

    Part 2

    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.

    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;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should numerical attributes in XML be quoted? <root> <node size=45 /> <foo bar=1.2> <baz
Should be a simple question for SDL experts. I am confused about the following
It seems like this should be fairly obvious, but I can't find anything on
Theres some pretty nasty XML that i'd like to unmarshall to a java object
I need to unmarshall an XML that has namespaces for the attributes, for instance
Should be easy. I have an object. I want to modify it, but before
should be a simple solution that I am missing. I have a Tab View
Should we declare the private fields as volatile if the instanced are used in
Should createUrl be called on controller or in views ? It doesn't matter? Or
Should I constantly have a session open in my web site with user accounts,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.