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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:15:34+00:00 2026-05-25T19:15:34+00:00

I am trying to use JAXB to serialize a HashTable<String, String> to XML. I

  • 0

I am trying to use JAXB to serialize a HashTable<String, String> to XML. I am very new to Java (came from C#), so I am kinda perplexed by this task.

I have seen the following code:

public static <T> String ObjectToXml(T object, Class<T> classType) throws JAXBException
{
  JAXBContext jaxbContext = JAXBContext.newInstance(classType);
  StringWriter writerTo = new StringWriter();
  Marshaller marshaller = jaxbContext.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
  marshaller.marshal(object, writerTo); //create xml string from the input object
  return writerTo.toString();
}

Which is invoked like so: ObjectToXml(o, ClassOfO.class), but HashTable<String, String>.class is wrong (that I already know).

Can Java gurus out there show me how to invoke this code? Proposing a simpler implementation (along with an invocation example, of course) is most welcome as well.

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-05-25T19:15:34+00:00Added an answer on May 25, 2026 at 7:15 pm

    You will need to create a wrapper class to hold onto the Hashtable:

    package forum7534500;
    
    import java.util.Hashtable;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Wrapper {
    
        private Hashtable<String, String> hashtable;
    
        public Hashtable<String, String> getHashtable() {
            return hashtable;
        }
    
        public void setHashtable(Hashtable<String, String> hashtable) {
            this.hashtable = hashtable;
        }
    
    }
    

    Then you can do the following:

    package forum7534500;
    
    import java.io.StringWriter;
    import java.util.Hashtable;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Wrapper.class);
            Wrapper wrapper = new Wrapper();
            Hashtable<String, String> hashtable = new Hashtable<String,String>();
            hashtable.put("foo", "A");
            hashtable.put("bar", "B");
            wrapper.setHashtable(hashtable);
            System.out.println(objectToXml(jc, wrapper));
        }
    
        public static String objectToXml(JAXBContext jaxbContext, Object object) throws JAXBException
        {
          StringWriter writerTo = new StringWriter();
          Marshaller marshaller = jaxbContext.createMarshaller();
          marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
          marshaller.marshal(object, writerTo); //create xml string from the input object
          return writerTo.toString();
        }
    
    }
    

    This will produce the following output:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <wrapper>
        <hashtable>
            <entry>
                <key>bar</key>
                <value>B</value>
            </entry>
            <entry>
                <key>foo</key>
                <value>A</value>
            </entry>
        </hashtable>
    </wrapper>
    

    Things to Note

    • JAXBContext is a thread-safe object and should be created once and reused.
    • Hashtable is synchronized, if you do not need this then using HashMap is the common replacement.
    • The convention is to start Java method names with a lower case letter.

    Customizing the Mapping

    You can use an XmlAdapter in JAXB to customize the mapping of any class. Below is an link to a post on my blog where I demonstrate how to do just that:

    • http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Jaxb in order to unamrshal an xml file. for some
Trying to use this following script to load a page so that I can
Im trying to use my web Config files in Silverlight. I've put the following
Im trying to use PHP mail on my local computer, but its just not
Im trying to use the DotNetOpenAuth library for openid authentication. I have used the
When trying to use i18n in rails I get the following undesired output. The
Im trying to use the Yahoo rich text editor in my web application. I'm
Im trying to use Enterprise Architect to model Oracle database. Everything is going fine,
Good morning , In Scott Meyers book Effective STL, Mr Meyers explains how the
In my program(C++, WinAPI), I wanted to simulate pressing some existing hotkeys set in

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.