title: Parsing HTML Data using Java (DOM parse)
body: <p>I've worked on this for a while and didn't find anything related on Stack Overflow. I'm using a parser that's intending on capturing snippets of HTML code. Based on the code (further below), the file grows exponentially in size and is capturing the fields (li) I need but is also very repetitive in that it's capturing the same data over and over again.</p>
  
  <p>Here's the file that I'm reading from (the full file actually has over 100 lines but only included 3 lines here for this post):</p>
  
  <pre><code>&lt;html xlmns=http://www.w3.org/1999/xhtml&gt;
  &lt;name&gt;Name: J0719&lt;/name&gt;
  &lt;bracket&gt;&lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts: 2&lt;/li&gt;&lt;li&gt;State: 3&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt; 
  &lt;name&gt;Name: J0716&lt;/name&gt;
  &lt;bracket&gt;&lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts: 3&lt;/li&gt;&lt;li&gt;State: 2&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt; 
  &lt;name&gt;Name: J0718&lt;/name&gt; 
  &lt;bracket&gt;&lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts: 1&lt;/li&gt;&lt;li&gt;State: 5&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt;
  &lt;name&gt;Name: J0726&lt;/name&gt;
  &lt;bracket&gt;&lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts: 8&lt;/li&gt;&lt;li&gt;State: 4&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>My full code is here:</p>
  
  <pre><code>package ReadXMLFile_part2;
  
  import java.io.*;
  
  import org.jsoup.Jsoup;
  import org.jsoup.select.Elements;
  import org.jsoup.nodes.Document;
  import org.jsoup.nodes.Element;
  
  
  import java.util.Enumeration;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  import javax.swing.text.MutableAttributeSet;
  import javax.swing.text.html.HTML.Tag;
  import javax.swing.text.html.HTMLEditorKit;
  import javax.swing.text.html.parser.ParserDelegator;
  
  public class ReadXMLFile_part2 {
  
  public static void main(String[] args) throws Exception {
  
  PrintStream out = new PrintStream(new FileOutputStream("C:/XML_UltraEdit/XML_Sandbox/NetBeans_Java_Project/results2.xml"));
  System.setOut(out);
  
  System.out.println("*** JSOUP ***");
  
  File input = new File("C:/XML_UltraEdit/XML_Sandbox/NetBeans_Java_Project/output2_TEST.html");
  Document doc = null;
      try {
          doc = Jsoup.parse(input,"UTF-8", "http://www.w3.org/1999/xhtml" );
      } catch (IOException ex) {
          Logger.getLogger(ReadXMLFile_part2.class.getName()).log(Level.SEVERE, null, ex);
      }
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  
  //For loops to capture the &lt;li&gt; fields in the file
  Element bracket = doc.getElementsByTag("bracket").first();
  Elements trs = bracket.getElementsByTag("description");
  for (Element description : trs) {
      for (Element li : description.getAllElements()) {
          System.out.println(li.text());
      }
  }
  System.out.println();
  
  //read a line from the console
  String lineFromInput = in.readLine();
  
  //output to the file a line
  out.println(lineFromInput);                                 
  out.close();    
  }
  
  }
  </code></pre>
  
  <p>My question is how do I parse through the fields marked by "li" in the input file such that my output file has a new line for each "li" tag. Ideal output would be to look like this (and prevent an infinite loop):</p>
  
  <pre><code>Name: J0719
  Hop Counts: 2
  State: 3
  Name: J0716
  Hop Counts: 3
  State: 2
  Name: J0718
  Hop Counts: 1
  State: 5
  Name: J0726
  Hop Counts: 8
  State: 4
  </code></pre>
  
  <p>Thanks and appreciate any help on this!</p>
  
  <p>Sep 2nd update:
  Although the previousElementSibling was useful when used alone but I required another nested loop of some sort when also attempting to pull out the "Description" fields (otherwise previousElementSibling just continuously pulled the first previous element each time). The much quicker workaround I found was to just change the tags around in the original code so that it now looks like the code below:</p>
  
  <p>Updated XML file:</p>
  
  <pre><code>&lt;html xlmns=http://www.w3.org/1999/xhtml&gt;
  &lt;bracket&gt;&lt;li&gt;Name: J0719&lt;/li&gt;
  &lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts 2&lt;/li&gt;&lt;li&gt;State: 3&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt;
  &lt;bracket&gt;&lt;li&gt;Name: J0716&lt;/li&gt;
  &lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts 3&lt;/li&gt;&lt;li&gt;State: 2&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt;
  &lt;bracket&gt;&lt;li&gt;Name: J0718&lt;/li&gt;
  &lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts 1&lt;/li&gt;&lt;li&gt;State: 5&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt;
  &lt;bracket&gt;&lt;li&gt;Name: J0719&lt;/li&gt;
  &lt;description&gt;Description: &lt;ol&gt;&lt;li&gt;Hop Counts 8&lt;/li&gt;&lt;li&gt;State: 4&lt;/li&gt;&lt;/eol&gt;&lt;/description&gt;&lt;/bracket&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Aside from the following 'for' loops, everything else from the original code remained the same</p>
  
  <pre><code>//Updated Code:
  //For loops to capture the (li) fields in the file
  Elements brackets = doc.getElementsByTag("bracket");
  
  
      for (Element bracket : brackets) {
          Elements lis = bracket.select("li");
  
              for (Element li : lis){
                  System.out.println(li.text());
  
          }
          break;
      }
      System.out.println();
  </code></pre>
  
  <p>The only other thing is that I have to manually press the 'stop' running button a while later after execution after i see the file size stops growing. But i still see the output file generating the desired results.</p>