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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:45:47+00:00 2026-05-18T06:45:47+00:00

I’m upgrading a Java object that currently has XML representation in this spirit: <myObjects>

  • 0

I’m upgrading a Java object that currently has XML representation in this spirit:

<myObjects>
    <myObject uid="42" type="someEnum">
        <name>Waldo</name>
        <description>yada yada</description>
        <myElement>some_string</myElement>
        ...
    </myObject>
    ...
</myObjects>

myElement is optional – it can be null in Java / omitted in XML.
I’m adding a field that is only relevant if myElement has an actual value
(and to keep compatibility with previous XML, it’s optional in itself)

I’m trying to avoid this:

    <myElement>some_string</myElement>
    <myAttr>foo</myAttr>

and prefer something like this:

    <myElement myAttr="foo">some_string</myElement>

but banging my head for 2 days now on how to annotate it.

  • I thought of marking it with XmlTransient and let an XmlAnyElement catch it instead while unmarshalling – but it seems this will cause a problem when marshalling the object back from Java to XML.
  • I tried creating an XmlAdapter for the element – but the unmarshal method gets only the inner content (“some_string”). Am I missing something with this technique?
  • Is there a way to just get the element as a string (“<myElement myAttr=\”foo\”>some_string</myElement>”) and I will process it myself?
  • Do you recommend any other approach?
  • 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-18T06:45:47+00:00Added an answer on May 18, 2026 at 6:45 am

    You can use the EclipseLink JAXB (MOXy) @XmlPath extension to easily accomplish this:

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class MyObject {
    
        @XmlAttribute
        private int uid;
    
        @XmlAttribute
        private String type;
    
        private String name;
    
        private String description;
    
        private String myElement;
    
        @XmlPath("myElement/@myAttr")
        private String myAttr;
    
    }
    

    This class will interact with the following XML:

    <myObject uid="42" type="someEnum">
        <name>Waldo</name>
        <description>yada yada</description>
        <myElement myAttr="foo">some_string</myElement>
    </myObject>
    

    Using the following demo code:

    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(MyObject.class);
    
            File file = new File("input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            MyObject myObject = (MyObject) unmarshaller.unmarshal(file);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(myObject, System.out);
        }
    
    }
    

    To specify MOXy as your JAXB implementation you need to include a file called jaxb.properties in the same package as your model classes with the following entry:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    For more information on MOXy’s XPath based mapping see:

    • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
    • http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.