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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:10:01+00:00 2026-06-15T03:10:01+00:00

I need to annotate a class for unmarshalling an XML like this: <element>data</element> And

  • 0

I need to annotate a class for unmarshalling an XML like this:

<element>data</element>

And I don’t know how to do it because the class has to be annotated with @XmlRootElement but I need to get the root element value. I know this doesn’t work but it’s what I have done:

@XmlRootElement(name = "element")
public Class MyClass{

@XmlElement(name = "element")
protected String elementValue;
public String getElementValue(){...}
public void setElementValue(String el){...}

Is there any possibility to get this?

  • 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-15T03:10:03+00:00Added an answer on June 15, 2026 at 3:10 am

    You are looking for the @XmlValue annotation.

    MyClass

    package forum13626828;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name = "element")
    public class MyClass{
    
        protected String elementValue;
    
        @XmlValue
        public String getElementValue() {
            return elementValue;
        }
    
        public void setElementValue(String el) {
            this.elementValue = el;
        }
    
    }
    

    Demo

    package forum13626828;
    
    import java.io.StringReader;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(MyClass.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StringReader xml = new StringReader("<element>data</element>");
            MyClass myClass = (MyClass) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(myClass, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <element>data</element>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is that I need to create in runtime a class like this:
<context:annotation-config/> <context:component-scan... this is used for class that i need to annotated with @Repository
I need to annotate a class that has a Set<UUID> property. Marshalling a scalar
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I want XML like this: <simple>Foo</simple> I can do this successfully via a JAXB
Since I want to generate Scala code from an annotated Scala class, I need
Need: take in XML and save data to database. Currently using: JAXB to convert
I have a model like this: class Task(models.model): TASK_STATUS_CHOICES = ( (uP, u'Pending'), (uA,
I need to do getKey() with this kind of Entity: @Entity public class Value
I need to annotate an image with Chinese Text and I am using Imagick

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.