2012-07-19 14 views
7

में मैं XML में CDATA tpyes पार्स करने के लिए कोशिश कर रहा हूँ। कोड ठीक चलाता है और यह लिंक प्रिंट होगा: कंसोल में (लगभग 50 गुना है, क्योंकि कितने लिंक मेरे पास नहीं है कि), लेकिन लिंक दिखाई नहीं देगा ... यह सिर्फ एक खाली सांत्वना जगह है। क्या मैं याद किया जा सकता ``पढ़ना CDATA एक्सएमएल जावा

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

क्या आप नमूना एक्सएमएल प्रदान कर सकते हैं? या उसका एक हिस्सा? – Sujay

+0

एक्सएमएल जोड़ा गया। मैं http URL को "videoURL" टैग में प्राप्त करने का प्रयास कर रहा हूं। – Matt

+0

क्या आप सुनिश्चित हैं कि आपके पास केवल एक बच्चा नोड 'नोड बच्चा = e.getFirstChild();' ? सभी बच्चे नोड्स प्राप्त करें और उन्हें डीबगर में निरीक्षण करें। –

उत्तर

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

यह काम किया। धन्यवाद सुजय! – Matt

+0

मदद करने में खुशी हुई! अगर मदद मिली तो जवाब स्वीकार करने पर विचार करें :) – Sujay

+0

आप सबसे अच्छे हैं –

संबंधित मुद्दे