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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:40:38+00:00 2026-05-16T23:40:38+00:00

I am trying to unmarshall data from an XML object in Java using SimpleXML.

  • 0

I am trying to unmarshall data from an XML object in Java using SimpleXML. The point of the whole thing was to convert an API from JAXB to SimpleXML. Therefore, I used the annotation way of parsing the document. Here is some code:

In the User class:

@Element(name="created", required=false)
private Date created;

The programmer who wrote the API used a DateAdapter to turn the String pulled in from the XML directly into a date. I attempted to convert it to SimpleXML. My assumption is that a Transformer used the same approach. Here is that code before and after…

Before:

public class DateAdapter extends XmlAdapter<String, Date> {

  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  public Date unmarshal(String date) throws Exception {
    return df.parse(date);
  }

  public String marshal(Date date) throws Exception {
    return df.format(date);
  }
}

After:

public class DateAdapter implements Transform<Date> {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  @Override
  public Date read(String date) throws Exception {
    return df.parse(date);
  }

  @Override
  public String write(Date date) throws Exception {
    return df.format(date);
  }
}

I assume I did the conversion wrong because I am now getting Unparsable date errors. The weird part is that, even if I put if-else or try-catch blocks within the read and write methods I still get the error.

So I think the main question is, how do I properly write an adapter like the JAXB one to marshall/unmarshall between the String from the XML and a Date object.

  • 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-16T23:40:39+00:00Added an answer on May 16, 2026 at 11:40 pm

    I fixed the problem. It had nothing to do with the API I was working on itself, but rather the conversion of a double to a date when unmarshalled by the actually XML framework. The double stored in the XML was in Epoch time in seconds, with decimals for milliseconds. SimpleXML could not seem to handle the decimals properly. My workaround was to just ask for a double instead of having the data be unmarshalled into a Date. So I got rid of the whole adapter and just changed

    @Element(name="created", required=false)
    private Date created;
    

    to

    @Element(name="created", required=false)
    private double created;
    

    I’m sure that there is a way with Simple to convert this into a Date after the fact, but I figure it’s better to just leave it as is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create java objects from xml file. I am using jaxb(unmarshalling)
I am trying to unmarshall a XML document created from jersey JAXB annotated class.
I have some xml data I'm trying to unmarshall into java objects and one
I am trying to unmarshall the following XML using JAXB: <Works> <Work> <Composers> <Composer>
I am trying to create objects using binding data from xml file to classes
Theres some pretty nasty XML that i'd like to unmarshall to a java object
I'm trying to unmarshal a simple xml document from a public api from Convio.
I'm trying to use JAXB to unmarshall some XML, but I'm getting an Unable
I am trying deserialize XML data into newly created Java content trees: I am
I'm trying to use JAXB to unmashall some XML which I used xjc to

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.