title: Java XML: ClassCastException DeferredTextImpl
body: <p>Here is my code:</p>
  
  <pre><code>// get the factory
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  
  try {
  
      // Using factory get an instance of document builder
  DocumentBuilder db = dbf.newDocumentBuilder();
  
  // parse using builder to get DOM representation of the XML file
      dom = db.parse(file);
  
  } catch (ParserConfigurationException pce) {
      pce.printStackTrace();
  } catch (SAXException se) {
      se.printStackTrace();
  } catch (IOException ioe) {
      ioe.printStackTrace();
  }
  
  NodeList n1 = dom.getChildNodes();
  Element e1 = (Element) n1.item(0);
  
  System.out.println(n1.getLength());
  System.out.println(e1.getNodeName());
  
  NodeList n2 = n1.item(0).getChildNodes();
  Element e2 = (Element) n2.item(0);   //Line 61
  
  System.out.println(n2.getLength());
  System.out.println(e2.getNodeName());
  </code></pre>
  
  <p>Here is my XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;test-fw:test
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:test-fw="http://simitar/test-fw"&gt; 
  
  &lt;rule-tree&gt;
  &lt;rule class="matchlines"&gt;
  &lt;property name="contiguous"&gt; true&lt;/property&gt;
  &lt;property name="inOrder"&gt;false&lt;/property&gt;
  &lt;property name="exact"&gt;false&lt;/property&gt;
  &lt;property name="lines"&gt;modelInstantiated&lt;/property&gt;
  &lt;/rule&gt;
  &lt;rule class="matchlines"&gt;
  &lt;property name="contiguous"&gt; true&lt;/property&gt;
  &lt;property name="inOrder"&gt;true&lt;/property&gt;
  &lt;property name="exact"&gt;false&lt;/property&gt;
  &lt;property name="lines"&gt;InitEvent&lt;/property&gt;
  &lt;/rule&gt;
  &lt;/rule-tree&gt;
  
  &lt;/test-fw:test&gt;
  </code></pre>
  
  <p>Here is my output:</p>
  
  <pre><code>1
  test-fw:test
  Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
      at testpack.Main.run(Main.java:61)
      at testpack.Main.main(Main.java:86)
  </code></pre>
  
  <p>I keep getting this error. I am completely lost. I have no idea what to do. I want to able to have one node, and be able to grab all it's children and put them into an array or list, so I can iterate through them.</p>
  
  <p>Here are all my imports:</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.List;
  import java.util.Stack;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  import org.xml.sax.SAXException;
  </code></pre>
  
  <p>I've had the hardest time trying to get this Java to parse this XML file.</p>