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 8807617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:24:41+00:00 2026-06-14T02:24:41+00:00

I have an issue using JaxB in Netbeans 7.1.2. I have auto-generated my classes

  • 0

I have an issue using JaxB in Netbeans 7.1.2.

I have auto-generated my classes from a schema using JaxB (New JaxB Binding). I am creating the object that will be serialized to an XML string using the Marshaller and then back to a new object instance from the XML String. However, I get the following exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.somewhere.com/some/path", local:"MyQueryComplexTypeQuery"). Expected elements are (none)

The marshalling/serializing to XML string works fine. It’s when it is unmarshalled/deserialized that is causing the issue.

I am using the following code to build the object and marshal it to an XML string:

// build the object
ObjectFactory of = new ObjectFactory();

MyQueryComplexType oaaqc = of.createMyQueryComplexType();
oaaqc.setField1("edit");
oaaqc.setField2("net");
oaaqc.setField3("24");

JAXBElement<MyQueryComplexType> createMyQueryComplexType = of.createMyQueryComplexTypeQuery(oaaqc);

// serialise to xml
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(MyQueryComplexType.class);            
Marshaller m = context.createMarshaller();
m.marshal(createMyQueryComplexType, writer);

// output string to console
String theXML = writer.toString();
System.out.println(theXML);

This produces the following XML (formatted) in the console:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyQueryComplexTypeQuery xmlns="http://www.somewhere.com/some/path">
    <Field1>edit</Field1>
    <Field2>net</Field2>
    <Field3>24</Field3>
</MyQueryComplexTypeQuery>

Now I come to deserialize/unmarshal the string to a new instance MyQueryComplexType with the following code:

Unmarshaller u = context.createUnmarshaller();
MyQueryComplexTypeQuery o = (MyQueryComplexType) u.unmarshal(new StringReader(theXML));

In the auto generated package-info.java it has the following contents:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.somewhere.com/some/path", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package MyProject.SomeNamespace.MyQuery;

I thought the simple process of object -> string -> object would work. This is the first time I’ve used JaxB (so be gentle). I have seen other posts mentioning the namespaces, and everything looks ok to me. I’ve auto generated the classes from the schema, constructed the object, marshalled to xml string. I thought simply reversing the process for unmarshal would be similar.

The actual exception is thrown on line:

MyQueryComplexTypeQuery o = (MyQueryComplexType) u.unmarshal(new StringReader(theXML));

I thought I was doing the unmarshalling from string to object would be simple. I don’t know if I am doing something wrong or missing something. I hope you guys can shed some light or open my eyes.

I have cut down the code to simplify what is in my app that causes the error. Namespaces have been changed to protect the identity of things on the web.

Any thoughts?

Thanks

Andez

  • 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-14T02:24:42+00:00Added an answer on June 14, 2026 at 2:24 am

    Creating the JAXBContext

    For JAXB models that are generated from an XML schema you could create the JAXBContext on the package name rather than on a particular class:

    JAXBContext context = JAXBContext.newInstance("your.package.name");
    

    Not:

    JAXBContext context = JAXBContext.newInstance(MyQueryComplexType.class);  
    

    This will ensure that the ObjectFactory class that contains the @XmlElementDecl annotations is processed correctly which is what is missing from your use case.

    Unmarshal Operation

    Note that during your marshal operation you needed to wrap your instance of MyQueryComplexType in a JAXBElement? This is because MyQueryComplexType was not annotated with @XmlRootElement and the root element information needed to come from the JAXBElement. This means that when you do the unmarshal operation you will get a JAXBElement back as well.

    JAXBElement<MyQueryComplexTypeQuery> o = (JAXBElement<MyQueryComplexType>) u.unmarshal(new StringReader(theXML));
    

    Not:

    MyQueryComplexTypeQuery o = (MyQueryComplexType) u.unmarshal(new StringReader(theXML));
    

    For More Information

    • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an odd issue while using FlashCS4. I have a textfield that, when
i have an issue with synchronizing Master/slaves processes using MPI. I wish that the
I have this issue using assembly MIPS. From the keyboard I read an integer
Working on my 1.6.0_16 JDK, I generated my stub classes from a WSDL using
I have an issue using Ajax upload with Spring 3 MVC. I understand that
I have a weird issue using polymorphism. I have a base class that implements
I have an issue using the protectedObservable custom binding below found at this link.
We have an issue using the PEAR libraries on Windows from PHP . Pear
I have an issue using an object property as jquery selector define([models/security/user, 'text!templates/security/registration.html'], function(SecurityUserModel,
I have an issue while using buttons inside form. I want that button to

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.