dbwebtek
dbwebtek

Reputation: 245

XML Parsing in Java

<exam>

<question type="multichoice">
   <name>
       <text>Demo Imported Question</text>
   </name>
   <questiontext format="html">
       <text>What is SEO?</text>
   </questiontext>
   <answer fraction="0">
  <text>Son of English Organization</text>
 <feedback><text>WRONG</text></feedback>


</answer>
   <answer fraction="100">
      <text>Search Engine Optimization</text>
      <feedback><text>Right on!</text></feedback>
   </answer>
   <answer fraction="0">
      <text>Silver of England Office</text>
      <feedback><text>Ooops!</text></feedback>
   </answer>
   <questiontext format="html">
       <text>How many stars are there on US flag ?</text>
   </questiontext>
   <answer fraction="0">
      <text>46</text>
     <feedback><text>WRONG</text></feedback>
   </answer>
   <answer fraction="100">
      <text>52</text>
      <feedback><text>Right on!</text></feedback>
   </answer>
   <answer fraction="0">
      <text>54</text>
      <feedback><text>Ooops!</text></feedback>
   </answer>

   </question>
</exam>

The above is a dummy test xml and I like to read in by Java code snippet and simply ouput as

Question : what is seo?
answer: Son of English Organizatio
answer: Search Engine Optimization
answer: Silvet of england Office

...likewise for the next question

Does anyone has a best way to do this ? Thanks so much

Upvotes: 1

Views: 397

Answers (4)

vtd-xml-author
vtd-xml-author

Reputation: 3377

VTD-XML is the XML API that combines a number of benefits (perforamnce, memory usage and ease of use).

Upvotes: 0

Amit Patel
Amit Patel

Reputation: 15985

There are many libraries like dom4j, jdom, xtream etc available for reading and manipulating xml data. I found dom4j very easy.

Upvotes: 0

darioo
darioo

Reputation: 47183

If you want a "standard" way to do it, I'd recommend using JAXB since it's part of Java SE 6.

First, create an xsd schema for your xml files. Then use the command line xjc tool found in Java SDK. This will generate Java JAXB classes according to your schema.

Afterwards, read on how to use JAXB to painlessly obtain elements you need from your XML file. A nice and simple tutorial can be found here.

Upvotes: 5

user467871
user467871

Reputation:

use DOM parser . Here is the link that best fits you How to read XML file in Java

Upvotes: 2

Related Questions