开发者

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">开发者_JS百科;
      <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


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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜