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

  • Home
  • SEARCH
  • 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 7645383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:55:41+00:00 2026-05-31T09:55:41+00:00

I have an application doing XML<->conversions using Jaxb and automatically generated classes with maven-jaxb2-plugin.

  • 0

I have an application doing XML<->conversions using Jaxb and automatically generated classes with maven-jaxb2-plugin.

Someplace deep in my schema, I have the possibility to enter “ANY” xml.

Update: this better describes my schema. Some known XML wrapping a totally unknown part (the “any” part).

<xs:complexType name="MessageType">
  <xs:sequence>
    <xs:element name="XmlAnyPayload" minOccurs="0">
        <xs:complexType>
            <xs:sequence>
                <xs:any namespace="##any"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="OtherElements">
        ....
</xs:sequence>

This maps (by jaxb) to a inner class like this.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "any"
})
public static class XmlAnyPayload {

    @XmlAnyElement(lax = true)
    protected Object any;

When I unmarshall the entire structure, it is no problem. The “Object any” will render into a org.apache.xerces.dom.ElementNSImpl. Now, I want to recreate the Java object manually and then go to XML. How do I take some random XML and put into the any (org.apache.xerces.dom.ElementNSImpl) element to be able to build up the Java object?

Also, the next case is when I have this element as java, I want to unmarshall this very part (to be able to extract the XML string of this element). But this is not possible. I get an exception about root elements. But it is not possible to annotate ElementNSImpl.

unable to marshal type "com.sun.org.apache.xerces.internal.dom.ElementNSImpl" as an element because it is missing an @XmlRootElement annotation

Do you have any suggestions on how to handle these problems?

  • 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-05-31T09:55:43+00:00Added an answer on May 31, 2026 at 9:55 am

    @XmlAnyElement(lax = true) means in plain English something like:

    Dear JAXB! If you have a mapping for this element, please unmarshal it
    into a Java object. If you don’t know this element, just leave it as a
    DOM element.

    This is exactly what is happening in your case. So if you want to actually unmarshal the content of this lax any, provide JAXB context with a mapping for the element you wish to unmarshal. The easiest way to do this is to annotate your class with @XmlRootElement

    @XmlRootElement(name="foo", namespace="urn:bar")
    public class MyClass { ... }
    

    Now when you create your JAXB context, add MyClass into it:

    JAXBContext context = JAXBContext.newInstance(A.class, B.class, ..., MyClass.class);
    

    In this case, if JAXB meets the {urn:bar}foo element in the place of that xs:any, it will know that this element is mapped onto MyClass and will try to unmarshal MyClass.

    If you are creating JAXB context based on the package name (you probably do), you can still add you class (say, com.acme.foo.MyClass) to it. The easiest way is to create a com/acme/foo/jaxb.index resource:

    com.acme.foo.MyClass
    

    And the add your package name to the context path:

    JAXBContext context = JAXBContext.newInstance("org.dar.gee.schema:com.acme.foo");
    

    There are other ways with ObjectFactory etc., but the trick with jaxb.index is probably the easiest one.

    Alternatively, instead of unmarshalling everything in one run, you can leave the content of xs:any as DOM and unmarshal it into the target object in a second unmarshalling with anothe JAXB context (which know your MyClass class). Something like:

    JAXBContext payloadContext = JAXBContext.newInstance(MyClass.class);
    payloadContext.createUnmarshaller().unmarshal((Node) myPayload.getAny());
    

    This approach is sometimes better, especially when you have a combination of container/payload schemas which are relatively independent. Depends on the case.

    All said above applies to marshalling as well. It’s all neatly bidirectional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doing some XML parsing in my application. I used the following
I have built an application that generates a large XML file using Linq-to-Entities and
I have a winforms application that is doing the following: on each query: Db
We have a business application written for Windows mobile. It has people doing inventories.
I'm doing some performance tuning and capacity planning for a low-latency application and have
I have a WCF service application and in this app I am doing calls
i am doing one small application , wheich have login functionality, in the user
I am doing some maintenance on a VB6 Windows application. I have a .Net
I'm doing an asp.net application with one page. In this page, I have one
Hi i am doing internal mail service application , i have the field called

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.