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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:07:19+00:00 2026-06-13T19:07:19+00:00

I am trying to use JAXB to parse the following XML . I removed

  • 0

I am trying to use JAXB to parse the following XML. I removed irrelevant parts. Unfortunately the original XML is generated by a third party application and I do not have the DTD or an XSD file available, so I am building my JAXB code by hand.

<add>
  <add-attr attr-name="UserName">
    <value type="string">User1</value>
  </add-attr>
  <add-attr attr-name="Name">
    <value type="structured">
      <component name="familyName">Doe</component>
      <component name="givenName">John</component>
    </value>
  </add-attr>
</add>

The problem is of course the <value> element. This can be a element with plain text, or if its attribute type is “structured, a list of <component> elements.

I have created two classes (value1 and value2) which implement the two options, but I cannot tell JAXB which one to use, because the elements are both named “value”. Is there any solution?

  • 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-13T19:07:20+00:00Added an answer on June 13, 2026 at 7:07 pm

    Option #1 – One Value Class

    You could have one Value class that includes a String property annotated with @XmlMixed.

    package forum13232991;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Value {
    
        @XmlAttribute
        String type;
    
        @XmlMixed
        List<String> value;
    
        @XmlElement(name="component")
        List<Component> components;
    
    }
    

    Option #2 – Multiple value Classes Via XmlAdapter

    If you want them to be separate classes you could leverage an XmlAdapter for this:

    ValueAdapter

    package forum13232991;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class ValueAdapter extends XmlAdapter<Value, Object> {
    
        @Override
        public Value marshal(Object object) throws Exception {
            Value value = new Value();
            if(object instanceof StructuredValue) {
                StructuredValue structuredValue = (StructuredValue) object;
                value.type = "structured";
                value.components = structuredValue.components;
            } else {
                StringValue stringValue = (StringValue) object;
                value.value.add(stringValue.value);
            }
            return value;
        }
    
        @Override
        public Object unmarshal(Value value) throws Exception {
            if("string".equals(value.type)) {
                StringValue stringValue = new StringValue();
                StringBuilder strBldr = new StringBuilder();
                for(String string : value.value) {
                    strBldr.append(string);
                }
                stringValue.value = strBldr.toString();
                return stringValue;
            } else {
                StructuredValue structuredValue = new StructuredValue();
                structuredValue.components = value.components;
                return structuredValue;
            }
        }
    
    }
    

    AddAttr

    package forum13232991;
    
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    public class AddAttr {
    
        @XmlJavaTypeAdapter(ValueAdapter.class)
        Object value;
    
    }
    

    For More Information

    • http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use JAXB to unmashall some XML which I used xjc to
I'm trying to use Jaxb in order to unamrshal an xml file. for some
I'm trying to use JAXB to unmarshall some XML, but I'm getting an Unable
I'm trying to use JAXB to pull some elements from a large XML file
I generated classes using xjc, and I am trying to process the following XML
I'm trying to use the @Catch annotation to catch any JAXB UnmarshalExceptions generated in
I'm having an awful time trying to use a library to parse an XML
I'm trying to use my objects with JAXB annotations for application/json output with my
I am trying to use JAXB's introspection to marshall and unmashall some existing domain
I am trying to use the Jaxb classes generator in eclipse to generate jaxb

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.