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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:31:49+00:00 2026-05-28T19:31:49+00:00

I want to unmarshall the following XML structure using JAXB: <orCondition> <andCondition> <andCondition> <simpleCondition

  • 0

I want to unmarshall the following XML structure using JAXB:

<orCondition>
    <andCondition>
      <andCondition>
        <simpleCondition value="val1" />
        <simpleCondition value="val2" />
      </andCondition>
      <simpleCondition value="val3" />
    </andCondition>
    <simpleCondition value="val4" />
</orCondition>

I have the following Java classes:

Condition base class

@XmlType(name="condition")
public class Condition {

}

Composite class containing two conditions

@XmlType(name="composite")
public class Composite extends Condition {

    @XmlElement(name = "condition", type = Condition.class)
    private Condition firstCondition;

    @XmlElement(name = "condition", type = Condition.class)
    private Condition secondCondition;

    public Condition getFirstCondition() {
        return firstCondition;
    }

    public void setFirstCondition(Condition firstCondition) {
        this.firstCondition = firstCondition;
    }

    public Condition getSecondCondition() {
        return secondCondition;
    }

    public void setSecondCondition(Condition secondCondition) {
        this.secondCondition = secondCondition;
    }
}

And condition

@XmlType(name = "andCondition")
public class And extends Composite {

}

Or condition

@XmlType(name = "orCondition")
public class Or extends Composite {

}

Simple leaf class condition

@XmlType(name = "simpleCondition")
public class Simple extends Condition {

    @XmlAttribute
    String value;
}

This does not work. I have an instance variable in an other class that looks like this:

@XmlElement(name = "condition", type = Condition.class) 
private Condition condition; 

It stays null after unmarshalling. The rest of the object unmarshalls fine

Anny suggestions? Or is this not possible?

  • 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-28T19:31:50+00:00Added an answer on May 28, 2026 at 7:31 pm

    You could structure your model like:

    Composite

    You could change your Composite class to look something like the following and use the @XmlElementRef annotation. @XmlElementRef corresponds to the XML schema concept of substitution groups:

    package forum9006785;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Composite extends Condition {
    
        @XmlElementRef
        private Condition[] condition = new Condition[2];
    
        public Condition getFirstCondition() {
            return condition[0];
        }
    
        public void setFirstCondition(Condition firstCondition) {
            condition[0] = firstCondition;
        }
    
        public Condition getSecondCondition() {
            return condition[1];
        }
    
        public void setSecondCondition(Condition secondCondition) {
            condition[1] = secondCondition;
        }
    
    }
    

    Condition

    package forum9006785;
    
    public class Condition {
    
    }
    

    And

    For each of the subclasses that can appear in the XML you will need to annotate with @XmlRootElement. This acts as the type identifier for the @XmlElementRef property.

    package forum9006785;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name = "andCondition")
    public class And extends Composite {
    
    }
    

    Or

    package forum9006785;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name = "orCondition")
    public class Or extends Composite {
    
    }
    

    Simple

    package forum9006785;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name = "simpleCondition")
    public class Simple extends Condition {
    
        @XmlAttribute
        String value;
    
    }
    

    Demo

    package forum9006785;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(And.class, Or.class, Simple.class);
    
            File xml = new File("src/forum9006785/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Condition condition = (Condition) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(condition, System.out);
        }
    
    }
    

    Input/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <orCondition>
        <andCondition>
            <andCondition>
                <simpleCondition value="val1"/>
                <simpleCondition value="val2"/>
            </andCondition>
            <simpleCondition value="val3"/>
        </andCondition>
        <simpleCondition value="val4"/>
    </orCondition>
    

    For More Information

    • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
    • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to unmarshall the following XML using JAXB: <Works> <Work> <Composers> <Composer>
I'm using JAXB to read and write XML. What I want is to use
want to know why String behaves like value type while using ==. String s1
I want to format a XML document during unmarshal with JAXB. Unmarshal looks like:
I have an application doing XML<->conversions using Jaxb and automatically generated classes with maven-jaxb2-plugin.
I'm trying to use JAXB to unmarshall some XML, but I'm getting an Unable
I use jaxb in my REST application. I want to send an XML file
I have the following XML structure, which is modelling a single concept across multiple
Want to know, having a class without any annotation, how the jaxb unmarshall the
I want to generate xml file from Struts action object. So that I have

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.