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

  • Home
  • SEARCH
  • 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 8632241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:19:28+00:00 2026-06-12T09:19:28+00:00

I need to generate xml using jaxb as below : <item> <key1 id=default>value1</key1> <key2

  • 0

I need to generate xml using jaxb as below :

<item>
    <key1 id="default">value1</key1>
    <key2 id="default">value2</key2>
    <key3 id="default">value3</key3>
</item>

how to do this using @XmlPath in jaxb?

i have used below one. But i have multiple keys around 50. how to acheive this?

   @XmlPath("key1/@id")
    private String attrValue = "default";
  • 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-12T09:19:29+00:00Added an answer on June 12, 2026 at 9:19 am

    @XmlPath is an extension in the EclipseLink MOXy implementation of JAXB (JSR-222). You will need to use the equivalent in MOXy’s mapping file to get the desired behaviour.

    oxm.xml

    What you are looking for is the ability to apply multiple writeable mappings for a field/property. This can’t currently be done via annotations, but can be done using MOXy’s external mapping document.

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum12704491">
        <java-types>
            <java-type name="Item">
                <java-attributes>
                    <xml-element java-attribute="attrValue" xml-path="key1/@id"/>
                    <xml-element java-attribute="attrValue" xml-path="key2/@id" write-only="true"/>
                    <xml-element java-attribute="attrValue" xml-path="key3/@id" write-only="true"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    Item

    package forum12704491;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Item {
    
        private String attrValue;
        private String key1;
        private String key2;
        private String key3;
    
    }
    

    jaxb.properties

    In order to specify MOXy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)

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

    Demo

    The demo code below demonstrates how to bootstrap using MOXy’s external mapping document.

    package forum12704491;
    
    import java.io.File;
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum12704491/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Item.class}, properties);
    
            File xml = new File("src/forum12704491/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Item item = (Item) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(item, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <item>
       <key1 id="default">value1</key1>
       <key2 id="default">value2</key2>
       <key3 id="default">value3</key3>
    </item>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this schema and i'm using JAXB to generate java stub files. <?xml
I need to generate a big xml. I am currently using JAXB 2.0 .
I need to generate XML in the following form: <ns1:root xmlns:ns1=http://example.com/xmlns1 xmlns:ns2=http://example.com/xmlns2> <ns2:item ns2:param=value
What tool do I need to generate XML intellisense help file as shown below?
I need to generate an xsl table for the xml below, for attributes fname
I need to generate XML structures on the fly using PHP. I've seen many
I'm trying to generate some XML using XML::Builder, but my element names need to
I am using this link to generate XML file using DOM. It says that
I am using PHP to generate some XML. I need to put quotes in
I have a service that needs to generate xml. Currently I am using 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.