title: Best way to extract data between several xml tags from a file
body: <hr>
  
  <pre><code>&gt; &lt;DOC&gt;
  &gt; &lt;Car&gt; Zen &lt;/Car&gt; 
  &gt; &lt;Description&gt; This is a bla bla model. which is a bla
  &gt; bla thisnf dsgs
  &gt; sdfsgssssssssssssssssssssssssssssssssssssssssssttttttttttttttwqqqqqqqqq
  &gt; dsgdsdsssssssssssegsegsdgsdgsdsssssssssssssssssssssttttttttttttt
  &gt; sdgssddddddddddddddddddddddddddddddddddddddddddddddsdddddddddwwww
  &gt; dgdssdddddddddddddddddddddddddddddsssssssssssssssssssssssswwwwwwwwwwww
  &gt; gdgdsssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeee
  &gt; gddsssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeqqqqqqqqqqq
  &gt; gsdsssssssssssssssssssssssssssssseqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
  &gt; dsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
  &gt; arrwerfkafjsdfsojfiosjfiosdfoisdoifjsdoifjiosdjfosdj&lt;/Description&gt;
  &gt; &lt;Year&gt; 2015 &lt;/Year&gt; &lt;Color&gt; Red &lt;/Color&gt; 
  &gt; &lt;Engine&gt; afsdf &lt;/Engine&gt;
  &gt; &lt;/DOC&gt;
  </code></pre>
  
  <p>---Some more tags----</p>
  
  <pre><code>&gt; &lt;DOC&gt;
  &gt; &lt;Car&gt; Zen1 &lt;/Car&gt; &lt;Description&gt; This is the second text tag which is a
  &gt; bla bla thisnf dsgs
  &gt; sdfsgssssssssssssssssssssssssssssssssssssssssssttttttttttttttwqqqqqqqqq
  &gt; dsgdsdsssssssssssegsegsdgsdgsdsssssssssssssssssssssttttttttttttt
  &gt; sdgssddddddddddddddddddddddddddddddddddddddddddddddsdddddddddwwww
  &gt; dgdssdddddddddddddddddddddddddddddsssssssssssssssssssssssswwwwwwwwwwww
  &gt; gdgdsssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeee
  &gt; gddsssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeqqqqqqqqqqq
  &gt; gsdsssssssssssssssssssssssssssssseqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
  &gt; dsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
  &gt; arrwerfkafjsdfsojfiosjfiosdfoisdoifjsdoifjiosdjfosdj&lt;/Description&gt;
  &gt; &lt;Year&gt; 2015 &lt;/Year&gt; &lt;Color&gt; Red &lt;/Color&gt; &lt;Engine&gt; afsdf &lt;/Engine&gt;
  &gt; &lt;/DOC&gt;
  </code></pre>
  
  <p>---Some more tags----</p>
  
  <p>I have several files, each file have many such tags within them.
  What is the best way to extract the data between each of   and   tags?
  This is my approach</p>
  
  <pre><code>&gt; for (File file : files) { 
  &gt; System.out.println(file.getName());   
  &gt; if
  &gt; (file.isFile()) {         
  &gt; try {
  &gt;       BufferedReader bufferReader = new BufferedReader(new FileReader(file));             &gt;       String line; 
  &gt;       XContentBuilder builder = null;
  &gt;       StringBuffer sb = null; 
  &gt;       String descrption= null; 
  &gt;       String car =null;   
  &gt;       Boolean within_description = false; 
  &gt;           String Car= "";
  &gt;           while ((line = bufferReader.readLine()) != null) {
  &gt;               // System.out.println(line);
  &gt;               if (line.equals("&lt;DOC&gt;")) {
  &gt;                   builder = jsonBuilder();
  &gt;                   sb = new StringBuffer();
  &gt;                   continue;
  &gt;               } 
  &gt;               
  &gt;       
  &gt;               if (line.contains("&lt;CAR&gt;")) {
  &gt;                    
  &gt;                   String remove_front_space = line.replaceAll("&lt;CAR&gt;",
  &gt;                           "");
  &gt;                   car= remove_front_space .replaceAll("&lt;/CAR&gt;",
  &gt;                           "").trim();
  &gt;                   builder = builder.startObject().field("CARR",
  &gt;                           car);
  &gt;                   continue;
  &gt;               }
  &gt;               if (line.equals("&lt;/DESCRIPTION&gt;")) {
  &gt;                   within_description = false;
  &gt;                   continue;
  &gt;               }
  &gt;               if (within_description) {
  &gt;                   sb.append(line);
  &gt;                   continue;
  &gt;               }
  &gt;               if (line.equals("&lt;DESCRIPTION&gt;")) {
  &gt;                   within_description = true;
  &gt;                   continue;
  &gt;               }
  &gt;               if (line.equals("&lt;/DOC&gt;")) {
  &gt;                   // JSONifying the string data
  &gt;                   text_toadd = sb.toString();
  &gt;                   builder = builder.field("text", text_toadd)
  &gt;                           .endObject();
  &gt;                   sb = null;
  </code></pre>
  
  <blockquote>
    <p>----Make database call and store CAR and DESCRIPTION information into Database.             </p>
  </blockquote>
  
  <pre><code>&gt;                 }     
  &gt;           bufferReader.close(); 
  &gt;           String json = builder.string(); 
  &gt;           System.out.println(json);
  &gt; 
  &gt;       } catch (IOException e) {           
  &gt;           e.printStackTrace();        
  &gt;       }
  &gt;   } }
  </code></pre>
  
  <p>Any suggestion would do.
  Thanks in advance!</p>