Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Hashtable, hashmap -> xml

Posted on 2003-03-21
7
Medium Priority
?
525 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

571 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