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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:51:04+00:00 2026-06-10T17:51:04+00:00

We’re building a many developer app using JAXB and keep stumbling on problems that

  • 0

We’re building a many developer app using JAXB and keep stumbling on problems that all come back to a “version” mismatch between producers and consumers of JAXB objects.

Process hasn’t alleviated the pain so I was thinking about something along the lines of CORBA object versioning for JAXB, maybe via a required final field whose values must match. As an added bonus I’d like to inject the version value as the Maven version # 🙂

This is all using annotations, no xsd.

Thoughts?

Thanks.

—– Clarification —–

Think of this as a Serializable serialVersionUID that is added to the marshal stream when the object is marshaled and is required and whose value is checked when the object is unmarshaled.

Various check rules can be implemented, but in this case I only want equality. If the current version of a Foo is 1.1 and you send me data to unmarshal whose version is anything other than 1.1 I will reject it.

Help?

  • 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-10T17:51:06+00:00Added an answer on June 10, 2026 at 5:51 pm

    You could do something like the following:

    Foo

    Add a version field to your root model object.

    package forum12218164;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    public class Foo {
    
        @XmlAttribute
        public static final String VERSION = "123";
    
        private String bar;
    
        public String getBar() {
            return bar;
        }
    
        public void setBar(String bar) {
            this.bar = bar;
        }
    
    }
    

    Demo

    In your demo code leverage a StAX parser to check the version attribute before determining if it is safe to perform the unmarshal operation:

    package forum12218164;
    
    import javax.xml.bind.*;
    import javax.xml.stream.*;
    import javax.xml.transform.stream.StreamSource;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            // Create the JAXBContext
            JAXBContext jc = JAXBContext.newInstance(Foo.class);
    
            // Create an XMLStreamReader on XML input
            XMLInputFactory xif = XMLInputFactory.newFactory();
            StreamSource xml = new StreamSource("src/forum12218164/input.xml");
            XMLStreamReader xsr = xif.createXMLStreamReader(xml);
    
            // Check the version attribute
            xsr.nextTag(); // Advance to root element
            String version = xsr.getAttributeValue("", "VERSION");
            if(!version.equals(Foo.VERSION)) {
                // Do something if the version is incompatible
                throw new RuntimeException("VERSION MISMATCH");
            }
    
            // Unmarshal for StAX XMLStreamReader
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Foo foo = (Foo) unmarshaller.unmarshal(xsr);
    
            // Marshal the Object
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(foo, System.out);
        }
    
    }
    

    VALID USE CASE

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <foo VERSION="123">
        <bar>ABC</bar>
    </foo>
    

    INVALID USE CASE

    input.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <foo VERSION="1234">
        <bar>ABC</bar>
    </foo>
    

    Output

    Exception in thread "main" java.lang.RuntimeException: VERSION MISMATCH
        at forum12218164.Demo.main(Demo.java:23)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.