title: How do I print out just certain elements from a text file that has xml tags to a new text file?
body: <p>I need help with something that sounds easy but has given me some trouble.</p>
  
  <p>I have a text file (record.txt) that has a root element 'PatientRecord' and sub tags in it ('first name', 'age', blood type, address etc...) that repeat over and over but with different values since it's a record for each person. I'm only interested in printing out the values in between the tags to a new text file for each person but only for the elements I want. For example with the tags I mentioned above I only want the name and age but not the rest of the info for that patient. How do I print out just those values separated by commas and then go to the next patient? 
  Here is the code I have so far</p>
  
  <pre><code>    package patient.records;
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  public class ProcessRecords {
  private static final String FILE = "C:\\Users\\Desktop\\records.txt";
  private static final String RECORD_START_TAG = "&lt;PatientRecord&gt;";
  private static final String RECORD_END_TAG = "&lt;/PatientRecord&gt;"; 
  private static final String newFileName = "C:\\Users\\Desktop\\DataFolder\\";    
  public static void main(String[] args) throws Exception {
      String scan;    
      FileReader file = new FileReader(FILE);
      BufferedReader br = new BufferedReader(file);
      Writer writer = null;
  
      while ((scan = br.readLine()) != null)        
      {            
          if (scan.contains(RECORD_START_TAG)) { 
  
  
              //This is the logic I am missing that will only grab the element values
              //between the tags inside of the file
  
              writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream(newFileName + "Record Data" + ".txt"), "utf-8"));             
              }      
          else if (scan.contains(RECORD_END_TAG)) {
              writer.close();
              writer=null;
          }
          else {
              // only write if writer is not null
             if (writer!=null) {
              writer.write(scan);
             }
          }            
      }       
      br.close();
      }   
  }   //This is the end of my code             
  </code></pre>
  
  <hr>
  
  <p>The text file (record.txt) I am reading in looks like this:</p>
  
  <pre><code>&lt;PatientRecord&gt; &lt;---first patient record---&gt;
  &lt;---XML Schema goes here---&gt;
              &lt;Info&gt;
                  &lt;age&gt;66&lt;/age&gt;
                  &lt;first_name&gt;john&lt;/first_name&gt;
                  &lt;last_name&gt;smith&lt;/last_name&gt;
                  &lt;mailing_address&gt;200 main street&lt;/mailing_address&gt;
                  &lt;blood_type&gt;AB&lt;/blood_type&gt;
                  &lt;phone_number&gt;000-000-0000&lt;/phone_number&gt;
  &lt;/PatientRecord&gt;
  &lt;PatientRecord&gt; &lt;---second patient record---&gt;
  &lt;---XML Schema goes here---&gt;
              &lt;Info&gt;
                  &lt;age&gt;27&lt;/age&gt;
                  &lt;first_name&gt;micheal&lt;/first_name&gt;
                  &lt;last_name&gt;thompson&lt;/last_name&gt;
                  &lt;mailing_address&gt;123 baker street&lt;/mailing_address&gt;
                  &lt;blood_type&gt;O&lt;/blood_type&gt;
                  &lt;phone_number&gt;111-222-3333&lt;/phone_number&gt;
  &lt;/PatientRecord&gt;
  </code></pre>
  
  <p>So in theory if I ONLY wanted to print out the values from the tags first name, mailing address, and blood type from this text file for all patients it should look like this:</p>
  
  <pre><code>john, 200 main street, AB
  //this line is blank
  michael, 123 baker street, O
  </code></pre>
  
  <p>Thanks for any and all help. If you feel like my code should be modified then I'm all for it. Thank you.</p>