?
Solved

Hashtable, hashmap -> xml

Posted on 2003-03-21
7
Medium Priority
?
518 Views
Last Modified: 2008-03-10
Hi there,

I don't know how difficult or tricky that could be so giving the maximum points.

Question : Is there a possibility to write the data of a hashtable or hashmap in to an xml file? or perhaps more important how has the mapping.xml file to look like?

My idea was to transform the hashtable into a hashset, because hashset inherrits from collection so that should not be a problem.

thx for helping

Randal
0
Comment
Question by:Randal_Flagg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8180356
What is your intention here - why do you want to do this?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8180679
>  Is there a possibility to write the data of a hashtable or hashmap in to an xml file?

  Yes there is. If you know how to construct an XML document then there should be no problem how you can read the data from a Hashtable object and put it into the XML file.

> or perhaps more important how has the mapping.xml file to look like?

  It depends on the data in the Hashtable and the hierarchy you want to have, although you will need to find a way to distinguish between parents and child elements if the structure is too complex. A very simple way of putting data from a Hashtable into an xml file is the following:

Hashtable table = new Hashtable();
table.put("first", new String("some data"));
table.put("second", new String("some other data"));

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("file.xml")));

out.println("<?xml version=\"1.2\"?>\n");
out.println("    <elements>\n");
for (Enumeration enum = table.keys(); enum.hasMoreElements(); )
{
   String key =  (String)enum.nextElement();
   out.println("        <" + key + ">\n");
   out.println("            " + (String) table.get(key) + "\n");
   out.println("        </" + key + ">\n");
}
out.println("    </elements>\n");
out.flush();
out.close();
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8184631
of course, if any of your keys or values have any non-valid chars in them, this won't work :-(

Maybe a better format would be:

for (Enumeration enum = table.keys(); enum.hasMoreElements(); )
{
  String key =  (String)enum.nextElement();
  out.println("        <key>\n");
  out.println("          <![CDATA[ " + key + "]]>\n");
  out.println("          <value>\n");
  out.println("            <![CDATA[ " + (String)table.get(key) + "]]>\n");
  out.println("          </value>\n");
  out.println("        </" + key + ">\n");
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 8184645
Errrr...mistake (as usual for me) ;-)

for (Enumeration enum = table.keys(); enum.hasMoreElements(); )
{
 String key =  (String)enum.nextElement();
 out.println("        <key>\n");
 out.println("          <![CDATA[ " + key + " ]]>\n");
 out.println("          <value>\n");
 out.println("            <![CDATA[ " + (String)table.get(key) + " ]]>\n");
 out.println("          </value>\n");
 out.println("        </key>\n");
}
0
 

Author Comment

by:Randal_Flagg
ID: 8193515
@CEHJ : The intention is to store information structured into a plain text file. So xml is a open format and standardised. Besides that if you've got references on other objects you can write them down as childs into the file. So this data can be easily read and stored back into the Objects. For example by tools like castor. But first I have to understand that on my own.
And I haven't to invent an other file format ;-) .

@thx for helping so fast :-D

Randal

0
 
LVL 35

Expert Comment

by:girionis
ID: 8193823
 Hmm.. Why exactly did you accept Tim's comment as an answer over mine? Are you using any special characters in your code? And if yes why didn't you state that in your original question?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8194253
>>For example by tools like castor.

and JAXB (free)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question