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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:08:02+00:00 2026-06-12T02:08:02+00:00

If there are some @XmlID annotated properties with equal value, even they are of

  • 0

If there are some @XmlID annotated properties with equal value, even they are of different type, JAXB could not unmarshall objects successfully, however marshall is good.

Is it a bug?

Or else, how to support that feature?

codes:

package xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public class MultiID {

    static public class LongXmlAdapter extends XmlAdapter<String, Long> {
        @Override
        public Long unmarshal(String s) {
            return Long.parseLong(s);
        }

        @Override
        public String marshal(Long number) {
            if (number == null) return "";

            return number.toString();
        }
    }


    static public class Element {
        private Long id;

        @XmlID
        @XmlAttribute(required=true)
        @XmlJavaTypeAdapter(LongXmlAdapter.class)
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }
    }
    static public class Component {
        private Long id;

        @XmlID
        @XmlAttribute(required=true)
        @XmlJavaTypeAdapter(LongXmlAdapter.class)
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        private Element elem;

        @XmlIDREF
        @XmlAttribute(required=true)
        public Element getElem() {
            return elem;
        }

        public void setElem(Element e) {
            this.elem = e;
        }
    }
    @XmlRootElement
    static public class Container {
        private List<Element> elemLst = new ArrayList<Element>();
        private List<Component> lst = new ArrayList<Component>();

        @XmlElementWrapper(name="elemList")
        @XmlElement(name="elem")
        public List<Element> getElemLst() {
            return (elemLst != null)?elemLst:(elemLst = new ArrayList<Element>());
        }

        public void setElemLst(List<Element> elemLst) {
            this.elemLst = elemLst;
        }

        @XmlElementWrapper(name="componentList")
        @XmlElement(name="component")
        public List<Component> getLst() {
            return (lst != null)?lst:(lst = new ArrayList<Component>());
        }

        public void setLst(List<Component> lst) {
            this.lst = lst;
        }
    }

    static public class XmlSerialization {
        public static Object read(String filepath, Class... classesToBeBound) throws FileNotFoundException {
            Object entity = null;
            try {
                File file = new File(filepath);
                if(!file.exists()) throw new FileNotFoundException(filepath);
                JAXBContext context = JAXBContext.newInstance(classesToBeBound);
                Unmarshaller um = context.createUnmarshaller();

                entity = um.unmarshal(file);
            } catch (JAXBException ex) {
                Logger.getLogger(XmlSerialization.class.getName()).log(Level.SEVERE, null, ex);
            }
            return entity;
        }

        public static void write(String filePath, Object entity, Class... classesToBeBound) {
            File file = new File(filePath);
            try {
                JAXBContext context = JAXBContext.newInstance(classesToBeBound);
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                m.marshal(entity, file);
            } catch (JAXBException ex) {
                Logger.getLogger(XmlSerialization.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
    public Container createContainer() {
        Container container = new Container();
        for(long i = 1 ; i < 5; ++i) {
            Element elem = new Element();
            elem.setId(i);
            container.getElemLst().add(elem);
            Component comp = new Component();
            comp.setId(i);
            comp.setElem(elem);
            container.getLst().add(comp);
        }
        return container;
    }
    public void write(String filePath, Container container) {
        XmlSerialization.write(filePath, container, Container.class, Component.class, Element.class);
    }
    public Container read(String filePath) {
        Container container = null;
        try {
            container = (Container)XmlSerialization.read(filePath, Container.class, Component.class, Element.class);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MultiID.class.getName()).log(Level.SEVERE, null, ex);
        }
        return container;
    }

    public static void main(String[] args) {
        MultiID test = new MultiID();
        String filePath = "c:\\tmp.xml";
        Container c1 = test.createContainer();
        test.write(filePath, c1);
        Container c2 = test.read(filePath);
        filePath = "c:\\tmp2.xml";
        test.write(filePath, c2);
    }
}

The outputs:

[tmp.xml]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<container>
    <elemList>
        <elem id="1"/>
        <elem id="2"/>
        <elem id="3"/>
        <elem id="4"/>
    </elemList>
    <componentList>
        <component id="1" elem="1"/>
        <component id="2" elem="2"/>
        <component id="3" elem="3"/>
        <component id="4" elem="4"/>
    </componentList>
</container>

[tmp2.xml]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<container>
    <elemList>
        <elem id="1"/>
        <elem id="2"/>
        <elem id="3"/>
        <elem id="4"/>
    </elemList>
    <componentList>
        <component id="1"/>
        <component id="2"/>
        <component id="3"/>
        <component id="4"/>
    </componentList>
</container>

Every “Component” in tmp2.xml has no element reference.
That’s the problem.
If we change component id starting from 100, for example, it will work as expect.

How to resolve this issue?
Thanks a lot.

  • 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-12T02:08:04+00:00Added an answer on June 12, 2026 at 2:08 am

    Your output is due to a bug in the implementation of JAXB (JSR-222) that you are using. Running your sample code with the JAXB impl included in JDK 1.7.0 for the Mac and EclipseLink JAXB (MOXy) I get the expected result for tmp2.xml.

    tmp2.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <container>
        <elemList>
            <elem id="1"/>
            <elem id="2"/>
            <elem id="3"/>
            <elem id="4"/>
        </elemList>
        <componentList>
            <component elem="1" id="1"/>
            <component elem="2" id="2"/>
            <component elem="3" id="3"/>
            <component elem="4" id="4"/>
        </componentList>
    </container>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are some scripts that do not work correctly if they check for changes.
Is there some way to define an abstract type as a parameter in an
Is there some possibility to generate sounds in C#? I mean not just beep
Is there some utility that could be called via command line to produce a
is there some 'where' type contraints in can add to make the follwing code
is there some simple way to inject Properties class loaded with a file from
Is there some implementation of java.util.Map that does not uses HashCode? I have the
Is there some formal standards that do not allow one to use the ^
There are some related questions about unpacking single-value tuples, but I'd like to know
Is there some type of data that is passed as _t ? I see

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.