Take the 2-minute tour ×

From a String like

<TestData Identifier=\"Test\" requiredAttribute=\"Present\"></TestData> <TestData Identifier=\"Test1\" requiredAttribute=\"Present1\"></TestData> <TestData Identifier=\"Test2\" requiredAttribute=\"Present2\"></TestData> <TestData Identifier=\"Test3\" requiredAttribute=\"Present3\"></TestData>

whats the best way to get the values of the attributes requiredAttribute i.e (Present,Present1,Present2...)

share|improve this question
This is probably like the most common question on this site. Do none of those Related questions ------> solve your problem? –  Java Drinker May 4 '11 at 19:40
Is it Faith Of Your Fathers that doesn't let you to type "java parse xml example" in Google? –  Vladimir Dyuzhev May 4 '11 at 19:42
add comment

3 Answers

You can look into JAXB unmarshalling. Check out this page for more details, it should point to what you need

http://jaxb.java.net/tutorial/section_3_1-Unmarshalling-and-Using-the-Data.html#Unmarshalling and Using the Data

share|improve this answer
add comment

For basic XML parsing like this, I've found NanoXML @ http://nanoxml.cyberelf.be/ to be about the easiest and mostlightweight.

Working with XML in Java send you down a long road to pain if you start using all the other libraries.

share|improve this answer
add comment

That's not XML - but you could do it with regex or by converting it to XML and parsing it out. The latter is probably more expensive. It depends on what the actual test data is and your requirements for it.

share|improve this answer
can you provide me one example of how to achieve this using regex ? –  OddOneOut May 4 '11 at 19:26
It depends. What data are you trying to capture? Just the attribute? –  Joseph Ottinger May 4 '11 at 19:29
yes exactly.... –  OddOneOut May 4 '11 at 19:30
Joseph, this is a bad, Baaaaaaad advice. The poor lad is on road to hell now... –  Vladimir Dyuzhev May 4 '11 at 19:44
Parsing xml and stuff like it is a solved problem. Don't suggest reinventing the wheel. –  Andrew Lazarus May 4 '11 at 20:36
show 1 more comment

Your Answer


