title: Reading CDATA XML in Java
body: <p>I'm trying to parse CDATA tpyes in XML. The code runs fine and it will print Links: in the console (about 50 times, because that's how many links I have) but the links won't appear...it's just a blank console space. What could I be missing?``    </p>
  
  <pre><code>package Parse;
  
  import java.io.File;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.CharacterData;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class XMLParse {
    public static void main(String[] args) throws Exception {
      File file = new File("c:test/returnfeed.xml");
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.parse(file);
  
      NodeList nodes = doc.getElementsByTagName("video");
      for (int i = 0; i &lt; nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        NodeList title = element.getElementsByTagName("videoURL");
        Element line = (Element) title.item(0);
        System.out.println("Links: " + getCharacterDataFromElement(line));
      }
    }
    public static String getCharacterDataFromElement(Element e) {
      Node child = e.getFirstChild();
      if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
      }
      return "";
    }
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Links: 
  
  Links: 
  
  Links: 
  
  Links: 
  
  Links: 
  
  Links: 
  
  Links: 
  </code></pre>
  
  <p>Sample XML: (Not full document)</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt; 
  &lt;response xmlns:uma="http://websiteremoved.com/" version="1.0"&gt;
  
      &lt;timestamp&gt;
          &lt;![CDATA[  July 18, 2012 5:52:33 PM PDT 
            ]]&gt; 
      &lt;/timestamp&gt;
      &lt;resultsOffset&gt;
          &lt;![CDATA[  0 
            ]]&gt; 
      &lt;/resultsOffset&gt;
      &lt;status&gt;
          &lt;![CDATA[  success 
          ]]&gt; 
      &lt;/status&gt;
      &lt;resultsLimit&gt;
          &lt;![CDATA[  207 
          ]]&gt; 
      &lt;/resultsLimit&gt;
      &lt;resultsCount&gt;
          &lt;![CDATA[  207 
          ]]&gt; 
      &lt;/resultsCount&gt;
      &lt;videoCollection&gt;
          &lt;name&gt;
              &lt;![CDATA[  Video API 
              ]]&gt; 
          &lt;/name&gt;
          &lt;count&gt;
              &lt;![CDATA[  207 
              ]]&gt; 
          &lt;/count&gt;
          &lt;description&gt;
              &lt;![CDATA[  
              ]]&gt; 
          &lt;/description&gt;
          &lt;videos&gt;
              &lt;video&gt;
                  &lt;id&gt;
                      &lt;![CDATA[  8177840 
                      ]]&gt; 
                  &lt;/id&gt;
                  &lt;headline&gt;
                      &lt;![CDATA[  Test1
                      ]]&gt; 
                  &lt;/headline&gt;
                  &lt;shortHeadline&gt;
                      &lt;![CDATA[  Test2
                      ]]&gt; 
                  &lt;/shortHeadline&gt;
                  &lt;description&gt;
                      &lt;![CDATA[ Test3
  
                      ]]&gt; 
                  &lt;/description&gt;
                  &lt;shortDescription&gt;
                      &lt;![CDATA[ Test4
  
                      ]]&gt; 
                  &lt;/shortDescription&gt;
                  &lt;posterImage&gt;
                      &lt;![CDATA[ http://a.com.com/media/motion/2012/0718/los_120718_los_bucher_on_howard.jpg
  
                      ]]&gt; 
                  &lt;/posterImage&gt;
                  &lt;videoURL&gt;
                      &lt;![CDATA[ http://com/removed/2012/0718/los_120718_los_bucher_on_howard.mp4
  
                      ]]&gt; 
                  &lt;/videoURL&gt;
              &lt;/video&gt;
          &lt;/videos&gt;
      &lt;/videoCollection&gt;
  &lt;/response&gt;
  </code></pre>