1

So I'm working on this application and writing this class to parse an xml. I've created a class that will represent my xml file, and I want to map some things for it and use the map later to extend the abilities of my parser. Heres an example:

I know that there is a DATE in every file, in the current version of XML files I'm parseing it is <date>dateValue</date> so I want to map my generic string "DATE" to "date" so I can simply reference a data structure (map) and get the value ("date") at a key value (in this case "DATE"). If thats all I wanted to do I'd just use a hashmap. However I've added something more complicated, I have a class to represent the data that I'm pulling out of the xml, say:

public class myXML{
   String date;
   setDate(String input){
        this.date = input;
   }
}

So I'd also like to figure out a way of attaching something else with the values "DATE", "date" and now the setDate("value"). So first is there a good way of linking 2 values to one key? Second is there a good way to map to a function and then use it after? Such as

function = map.getFunctionValue("DATE");
myXML.function(someValue);

All of this is sample code so please don't comment on the lack of constructor or syntax errors :)

Thanks ahead of time!

Jono
  • 3,393
  • 6
  • 33
  • 48
  • Have you investigated the XML parsing functionality that comes with the Java JDK (`javax.xml.*`)? You may be reinventing the wheel. – Jim Garrison Oct 29 '12 at 23:39
  • I do not get it completely. Perhaps you could use a container object for the hashmap? It can have several members and all to methods you want. Just carefully implement equals and hashcode methods. – tb- Oct 30 '12 at 00:05

2 Answers2

0

Apache Commons has a MultiMap. I don't see one in the standard library, unfortunately.

Based on the docs, it appears they don't include it because it isn't often needed.

A multimap is like a Map but it can map each key to multiple values. The Java Collections Framework doesn't include an interface for multimaps because they aren't used all that commonly. It's a fairly simple matter to use a Map whose values are List instances as a multimap.

As for the second question, Java sadly lacks first class functions, so there's no elegant way to do it. Various approaches you can take include using custom interfaces, wrapper classes, reflection, and bytecode hacks.

Antimony
  • 37,781
  • 10
  • 100
  • 107
0

Try to have a look at Xstream, got it from this related question

Community
  • 1
  • 1