I’m doing some work with the stackoverflow data set in Java and have a string like this:
<row Id="1" PostId="35314" Score="8" Text="not sure why this is getting
downvoted -- it is correct! Double check it in your compiler if you
don't believe him!" CreationDate="2008-09-06T08:07:10.730" UserId="1" />
(newlines added for readability)
Assuming the data above is in a String, what would be the most elegant way to convert it into a Map<String, String>, with the keys being the labels ("Id", "Score", …) and the values being Strings containing the values ("1", "35314", …)? I want to do this elegantly, readably, and succinctly because is this code will be seen by a lot of people. I wrote something up that does all kinds of string manipulation and it’s just ugly.
In the framework I am using, I have to process one row at a time, so I can’t parse the entire XML structure (all the lines) at once. I have to do one line at a time.
This will use the org.w3c.* libraries for processing. It isn’t as lightweight as a straightforward String processing approach is, so hopefully someone can come up with something better. Storing the DocumentBuilder as a static final variable would help speed up the processing, as you don’t need to create one every time.