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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:00:29+00:00 2026-06-17T16:00:29+00:00

I am trying to create a java test using XStream annotations to serialize/deserialize XML

  • 0

I am trying to create a java test using XStream annotations to serialize/deserialize XML like:

<book>
  <title>Hello</title>
  <author>Joe Smith</author>
  <publisher city="Smallville">
      <employees>25</employees>
      <age>15</age>
  </publisher>
  <key type="1">XYZ</key>
</book>

I have most of it working. I have a Book object which uses @XStreamAlias(“book”). I create and add a Publisher object to the book with the related alias and using the @XStreamAsAttribute annotation for city.

However, I get stuck when I try to create the object that represents the “key.” Basically I need an element named key with an attribute named “type” and the data.

in Book, if I use:

@XStreamAlias("key")
String key;

but that doesn’t give me the type attribute (obviously) and if

@XStreamAlias("type")
@XStreamAsAttribute
String type;

I get that attribute added to the Book object (ie,

<book type="1">
 ....
</book>

I haven’t found any examples showing how to add an attribute to a single element like this. Does XStream provide a way to do this?

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-17T16:00:30+00:00Added an answer on June 17, 2026 at 4:00 pm

    See http://fahdshariff.blogspot.de/2011/12/using-xstream-to-map-single-element.html (not my site)

    The answer adapted from there is:

    @XStreamAlias("error")
    @XStreamConverter(value=ToAttributedValueConverter.class, strings={"message"})
    public class Error {
    
       String message;
       int code;
    }
    

    This will produce the following XML:

    <error code="99">This is an error message</error>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run some spring test using Java and annotations configuration. I
I am trying to create a simple hello world test using JSF and CDI
I am trying to create a Java 7 address book using Eclipse and when
I am trying to create a GUI using java swing. From there I have
I'm trying to create a GUI using Java by hand-coding it (without using GUI
I'm trying to create an instant messenger in java using the SMACK API. My
I'm trying to create a Qt main windows from Java using JNI directly and
as an excercise I'm trying to create a metronome with Java using Thread.sleep as
I'm trying out log4j in a simple test app. I create a new Java
I'm trying to create some xlsx files using the Apache POI library for Java

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.