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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:04:04+00:00 2026-06-16T06:04:04+00:00

I have a schema that defines the following type: <xsd:complexType name=Payload> <xsd:sequence> <xsd:any namespace=##any

  • 0

I have a schema that defines the following type:

<xsd:complexType name="Payload">
   <xsd:sequence>
      <xsd:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
   </xsd:sequence>
</xsd:complexType>

And that creates an object like so:

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

    @XmlAnyElement(lax = true)
    protected List<Object> any;
}

Now I try adding another generated JAXB object to that Payload doing something like this:

Class payloadClass = ...;
JAXBContext context = JAXBContext.newInstance( WrapperRequest.class, payloadClass);
...
marshaller.marshal( wrappedRequest );

But I get a terrible exception that looks like it’ll never work so I decide to serialize the payload object to XML first then add that as a string in the payload.

StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance( sdoRequest.getClass() );
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(new JAXBElement(new QName("uri", sdoRequest.getClass().getSimpleName()), sdoRequest.getClass(), sdoRequest), writer);
payload.getAny().add( writer.toString() );

And this blows up with an exception saying “java.lang.String” does not contain an @XmlRootElement.

So how will the use of xs:any ever work with JAXB? Nothing seems to want to work because JAXB turns the Payload into Object, and it won’t serialize just anything in Object. This is all inside Axis2 as well so it’s been very challenging to get to this point.

  • 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-16T06:04:06+00:00Added an answer on June 16, 2026 at 6:04 am

    Below I will demonstrate JAXB (JSR-222) and any with an example:

    Payload

    The any property is annotated with @XmlAnyElement(lax=true). This means that for that property if an element is associated with a class via @XmlRootElement or @XmlElementDecl then an instance of the corresponding object will be used to populate the property if not the element will be set as an instance of org.w3c.dom.Element.

    package forum13941747;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Payload", propOrder = {
        "any"
    })
    public class Payload {
    
        @XmlAnyElement(lax = true)
        protected List<Object> any;
    
    }
    

    Foo

    Below is an example of a class annotated with @XmlRootElement.

    package forum13941747;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Foo {
    
    }
    

    Bar

    Below is an example of a class without the @XmlRootElement annotation. In this use case we will leverage the @XmlElementDecl annotation on a factory class (usually called ObjectFactory) annotated with @XmlRegistry.

    package forum13941747;
    
    public class Bar {
    
    }
    

    ObjectFactory

    Below is an example of specifying an @XmlElementDecl annotation for the Bar class.

    package forum13941747;
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;
    import javax.xml.namespace.QName;
    
    @XmlRegistry
    public class ObjectFactory {
    
        @XmlElementDecl(name="bar")
        public JAXBElement<Bar> createBar(Bar bar) {
            return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
        }
    
    }
    

    input.xml

    Below is the input document we’ll use for this example. There are 3 elements that correspond to the any property. The first corresponds to the @XmlRootElement annotation on the Foo class. The second corresponds to the @XmlElementDecl annotation for the Bar class and the third does not correspond to any of the domain classes.

    <?xml version="1.0" encoding="UTF-8"?>
    <payload>
        <foo/>
        <bar/>
        <other/>
    </payload>
    

    Demo

    In the demo code below we will unmarshal the input document, then output the classes of the objects in the resulting any property and then marshal the payload object back to XML.

    package forum13941747;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Payload.class, Foo.class, ObjectFactory.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum13941747/input.xml");
            Payload payload = (Payload) unmarshaller.unmarshal(xml);
    
            for(Object o : payload.any) {
                System.out.println(o.getClass());
            }
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(payload, System.out);
        }
    
    }
    

    Output

    Below is the output from running the demo code. Note the classes corresponding to the objects in the any property. The foo element became an instance of the Foo class. The bar element became an instance of JAXBElement that holds an instance of Bar. The other element became an instance of org.w3c.dom.Element.

    class forum13941747.Foo
    class javax.xml.bind.JAXBElement
    class com.sun.org.apache.xerces.internal.dom.ElementNSImpl
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <payload>
        <foo/>
        <bar/>
        <other/>
    </payload>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XSD schema with target namespace ns1 which defines the following type:
Lets say I have a schema that defines the following XML: <Values> <Add Key=Key1>Value
I have a database schema that is similar to the following: | User |
I have xsd schema file which I can't change. Here is an excerpt that
I have the following start of an XSD: <?xml version=1.0 encoding=UTF-8?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema xmlns:no=http://www.sychophants.com>
I have XSD Schema with the following structure: ElementA has only one ElementB and
I have xml file that defines some preference screens like the following example <PreferenceScreen
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
I have a schema design that looks like this: Table: User Row Column Family
I know that I should have schema of a table before calling NewRow method

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.