2012-07-19 90 views
7

我想解析XML中的CDATA tpyes。代碼運行良好,它將在控制檯中打印Links:(大約50次,因爲這是我有多少個鏈接),但鏈接不會出現......它只是一個空白的控制檯空間。我能怎麼會丟失``在Java中讀取CDATA XML

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 < 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 ""; 
    } 
} 

結果:

Links: 

Links: 

Links: 

Links: 

Links: 

Links: 

Links: 

示例XML:(不完整的文檔)

<?xml version="1.0" ?> 
<response xmlns:uma="http://websiteremoved.com/" version="1.0"> 

    <timestamp> 
     <![CDATA[ July 18, 2012 5:52:33 PM PDT 
      ]]> 
    </timestamp> 
    <resultsOffset> 
     <![CDATA[ 0 
      ]]> 
    </resultsOffset> 
    <status> 
     <![CDATA[ success 
     ]]> 
    </status> 
    <resultsLimit> 
     <![CDATA[ 207 
     ]]> 
    </resultsLimit> 
    <resultsCount> 
     <![CDATA[ 207 
     ]]> 
    </resultsCount> 
    <videoCollection> 
     <name> 
      <![CDATA[ Video API 
      ]]> 
     </name> 
     <count> 
      <![CDATA[ 207 
      ]]> 
     </count> 
     <description> 
      <![CDATA[ 
      ]]> 
     </description> 
     <videos> 
      <video> 
       <id> 
        <![CDATA[ 8177840 
        ]]> 
       </id> 
       <headline> 
        <![CDATA[ Test1 
        ]]> 
       </headline> 
       <shortHeadline> 
        <![CDATA[ Test2 
        ]]> 
       </shortHeadline> 
       <description> 
        <![CDATA[ Test3 

        ]]> 
       </description> 
       <shortDescription> 
        <![CDATA[ Test4 

        ]]> 
       </shortDescription> 
       <posterImage> 
        <![CDATA[ http://a.com.com/media/motion/2012/0718/los_120718_los_bucher_on_howard.jpg 

        ]]> 
       </posterImage> 
       <videoURL> 
        <![CDATA[ http://com/removed/2012/0718/los_120718_los_bucher_on_howard.mp4 

        ]]> 
       </videoURL> 
      </video> 
     </videos> 
    </videoCollection> 
</response> 
+0

你能否提供一個樣本xml?或其一部分? – Sujay 2012-07-19 03:58:32

+0

XML已添加。我試圖在「videoURL」標記中獲取http URL。 – Matt 2012-07-19 04:10:59

+0

你確定你只有一個子節點'Node child = e.getFirstChild();' ?獲取所有子節點並在調試器中檢查它們。 – 2012-07-19 05:28:38

回答

12

而不是檢查的第一個孩子,這將是審慎節點是否也有其他孩子。在你的情況下(我想如果你已經調試過這個節點,你會知道的),傳遞給方法getCharacterDataFromElement的節點有多個子節點。我更新了代碼,這個可能會給你指向正確方向的指針:

public static String getCharacterDataFromElement(Element e) { 

    NodeList list = e.getChildNodes(); 
    String data; 

    for(int index = 0; index < list.getLength(); index++){ 
     if(list.item(index) instanceof CharacterData){ 
      CharacterData child = (CharacterData) list.item(index); 
      data = child.getData(); 

      if(data != null && data.trim().length() > 0) 
       return child.getData(); 
     } 
    } 
    return ""; 
} 
+0

這工作。謝謝Sujay! – Matt 2012-07-20 02:44:09

+0

很高興幫助!請考慮接受答案,如果它幫助:) – Sujay 2012-07-20 03:37:32

+0

你是最好的 – 2016-07-07 09:18:32