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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:06:50+00:00 2026-06-13T07:06:50+00:00

Let’s say I have class Person: class Person{ String firstName; String lastName; String email;

  • 0

Let’s say I have class Person:

class Person{
  String firstName;
  String lastName;
  String email;
}

XML has format:

<person>
 <firstName value="asd" /> 
 <lastName value="bcd" />
 <email value="qwe" />
</person>

I can unmarshal/marshal this class using own XmlAdapter implementation for each
of field FirstNameAdapter, LastNameAdapter, EmailAdapter. As you can see each field represented in similar way – field name as xml element and field value as element’s attribute. Is it possible to create “universal” adapter to which I’ll be able to transfer name of the field and it will extract value of that field ?

P.S. I know about MOXy JAXB implementation, but I’m wondering if it is possible by means of reference JAXB implementation.

Thanks!

  • 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-13T07:06:51+00:00Added an answer on June 13, 2026 at 7:06 am

    You can use an XmlAdapter like this:

    import java.io.*;
    import javax.xml.bind.*;
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.*;
    
    @XmlType
    class Valued {
        @XmlAttribute(name="value")
        public String value;
    }
    
    class ValuedAdapter extends XmlAdapter<Valued, String> {
        public Valued marshal(String s) {
            Valued v = new Valued();
            v.value = s;
            return v;
        }
        public String unmarshal(Valued v) {
            return v.value;
        }
    }
    
    @XmlRootElement
    class Person {
    
        @XmlJavaTypeAdapter(ValuedAdapter.class)
        @XmlElement
        String firstName;
    
        @XmlJavaTypeAdapter(ValuedAdapter.class)
        @XmlElement
        String lastName;
    
    }
    
    class SO12928971 {
        public static void main(String[] args) throws Exception {
            Person p = new Person();
            p.firstName = "John";
            p.lastName = "Doe";
            JAXBContext jc = JAXBContext.newInstance(Person.class);
            StringWriter sw = new StringWriter();
            jc.createMarshaller().marshal(p, sw);
            String xml = sw.toString();
            System.out.println(xml);
            StringReader sr = new StringReader(xml);
            p = (Person)jc.createUnmarshaller().unmarshal(sr);
            assert "John".equals(p.firstName);
            assert "Doe".equals(p.lastName);
        }
    }
    

    The idea here is that XML Schema and therefore also JAXB has a clear distinction between element names and content types, even though many documents have a clear one-to-one correspondence between these two. So in the above code, the type Valued describes something that has a value attribute, without regards for the element name. The members you want to serialize are annotated as @XmlElement with no name included in that annotation. So they will generate elements with a name derived from the name of the member. The @XmlJavaTypeAdapter annotation will cause the serializer to treat these members as if their types vere Valued. So that is what their XML content type will be.

    The schema for the above code looks like this:

    <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="person" type="person"/>
    
      <xs:complexType name="person">
        <xs:sequence>
          <xs:element name="firstName" type="valued" minOccurs="0"/>
          <xs:element name="lastName" type="valued" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="valued">
        <xs:sequence/>
        <xs:attribute name="value" type="xs:string"/>
      </xs:complexType>
    </xs:schema>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let me explain best with an example. Say you have node class that can
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say, I have some user input (as a String) like 11010011011. Now I
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say you have a method that expects a numerical value as an argument.
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say you have a class, with certain properties, and that you tried your
Let's say I have two objects, Master and Slave . Slave has a method

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.