2013-07-17 19 views
0

我可以知道如何檢索![CDATA [for Java(android/eclipse))? 我想從代碼中檢索圖像,並使用Eclipse軟件在Android應用程序中顯示它。我如何從![CDATA [在JAVA(android/eclipse)中檢索圖像

MainActivity.java是我的代碼。 需要諮詢/幫助。

謝謝

<description><![CDATA[ 
<img src="http://l.yimg.com/a/i/us/we/52/38.gif"/><br /> 
<b>Current Conditions:</b><br /> 
Thunder in the Vicinity, 32 C<BR /> 
<BR /><b>Forecast:</b><BR /> 
Tue - Scattered Thunderstorms. High: 31 Low: 25<br /> 
Wed - Scattered Thunderstorms. High: 31 Low: 25<br /> 
Thu - Scattered Thunderstorms. High: 31 Low: 25<br /> 
Fri - Isolated Thunderstorms. High: 31 Low: 25<br /> 
Sat - Scattered Thunderstorms. High: 31 Low: 25<br /> 
<br /> 
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Singapore__SG/*http://weather.yahoo.com/forecast/SNXX0006_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> 
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/> 
]]></description> 

你MainActivity.java

public class MainActivity extends Activity { 

TextView weather; 
ImageView image; 

class MyWeather{ 
    String description; 

String image; 


    String conditiontext; 
    String conditiondate; 

    String numberOfForecast; 
    String forecast; 

    public String toString(){ 

    return "\n- " 
+ "image" + "\n" 

    + "Condition: " + conditiontext + "\n" 
    + conditiondate +"\n" 

    + "\n" 
    + "number of forecast: " + numberOfForecast + "\n" 
    + forecast; 

    } 
} 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     weather = (TextView)findViewById(R.id.weather); 
     image = (ImageView)findViewById(R.id.image); 

     Thread myThread = new Thread(new Runnable(){ 

    @Override 
    public void run() { 
    String weatherString = QueryYahooWeather(); 
      Document weatherDoc = convertStringToDocument(weatherString); 

      final MyWeather weatherResult = parseWeather(weatherDoc); 
      runOnUiThread(new Runnable(){ 

    @Override 
    public void run() { 
     weather.setText(weatherResult.toString()); 
    }}); 

    }}); 
     myThread.start(); 
    } 

    private MyWeather parseWeather(Document srcDoc){ 

    MyWeather myWeather = new MyWeather(); 

    //<description>Yahoo! Weather for New York, NY</description> 
    myWeather.description = srcDoc.getElementsByTagName("description") 
     .item(0) 
     .getTextContent(); 



    //<yweather:condition.../> 
    Node conditionNode = srcDoc.getElementsByTagName("yweather:condition").item(0); 
    myWeather.conditiontext = conditionNode.getAttributes() 
     .getNamedItem("text") 
     .getNodeValue() 
     .toString(); 
    myWeather.conditiondate = conditionNode.getAttributes() 
     .getNamedItem("date") 
     .getNodeValue() 
     .toString(); 

    //Added to get elements of <yweather:forecast.../> 
    NodeList forecastList = srcDoc.getElementsByTagName("yweather:forecast"); 

    myWeather.forecast = ""; 
    if(forecastList.getLength() > 0){ 
     myWeather.numberOfForecast = String.valueOf(forecastList.getLength()); 
     for(int i = 0; i < forecastList.getLength(); i++){ 
     Node forecastNode = forecastList.item(i); 
     myWeather.forecast += 
     forecastNode 
      .getAttributes() 
      .getNamedItem("date") 
      .getNodeValue() 
      .toString() + " " + 
     forecastNode 
      .getAttributes() 
      .getNamedItem("text") 
      .getNodeValue() 
      .toString() + 
     " High: " + forecastNode 
      .getAttributes() 
      .getNamedItem("high") 
      .getNodeValue() 
      .toString() + 
     " Low: " + forecastNode 
      .getAttributes() 
      .getNamedItem("low") 
      .getNodeValue() 
      .toString() + "\n"; 
     } 
    }else{ 
     myWeather.numberOfForecast = "No forecast"; 
    } 

    return myWeather; 
    } 

    private Document convertStringToDocument(String src){ 

    Document dest = null; 
    DocumentBuilderFactory dbFactory = 
     DocumentBuilderFactory.newInstance(); 
    DocumentBuilder parser; 

    try { 
     parser = dbFactory.newDocumentBuilder(); 
     dest = parser.parse(new ByteArrayInputStream(src.getBytes())); 
    } catch (ParserConfigurationException e1) { 
     e1.printStackTrace(); 
     Toast.makeText(MainActivity.this, 
     e1.toString(), Toast.LENGTH_LONG).show(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
     Toast.makeText(MainActivity.this, 
     e.toString(), Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Toast.makeText(MainActivity.this, 
     e.toString(), Toast.LENGTH_LONG).show(); 
    } 

    return dest; 
    } 

    private String QueryYahooWeather(){ 

    String qResult = ""; 
    String queryString = "http://weather.yahooapis.com/forecastrss?w=1062617&u=c"; 

    HttpClient httpClient = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(queryString); 

    try { 
     HttpEntity httpEntity = httpClient.execute(httpGet).getEntity(); 

     if (httpEntity != null){ 
     InputStream inputStream = httpEntity.getContent(); 
     Reader in = new InputStreamReader(inputStream); 
     BufferedReader bufferedreader = new BufferedReader(in); 
     StringBuilder stringBuilder = new StringBuilder(); 

     String stringReadLine = null; 

     while ((stringReadLine = bufferedreader.readLine()) != null) { 
     stringBuilder.append(stringReadLine + "\n"); 
     } 

     qResult = stringBuilder.toString(); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     Toast.makeText(MainActivity.this, 
     e.toString(), Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Toast.makeText(MainActivity.this, 
     e.toString(), Toast.LENGTH_LONG).show(); 
    } 

    return qResult; 
    } 

} 

回答

1

您可以使用DOM解析器解析能解密標記,然後UE正則表達式的字符串中提取網址。您也可以使用其他解析器。以下是如何使用dom和檢索圖像url的示例。

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();  
Document doc = builder.parse(open);// open is the xml file to parse 
NodeList title = doc.getElementsByTagName("description"); 
Element line = (Element) title.item(0); 
Log.i("CDATA Content ","" + getCharacterDataFromElement(line)); 
Matcher matcher = Pattern.compile("<img src=\"([^\"]+)").matcher(getCharacterDataFromElement(line)); 
while (matcher.find()) { 
Log.i("img url: " ,""+ matcher.group(1)); 
} 

getCharacterDataFromElement

private String getCharacterDataFromElement(Element line) { 
    // TODO Auto-generated method stub 
Node child = line.getFirstChild(); 
    if (child instanceof CharacterData) { 
    CharacterData cd = (CharacterData) child; 
    return cd.getData(); 
    } 
    return ""; 
} 

logcat的輸出

CData Content:(1508): <img src="http://l.yimg.com/a/i/us/we/52/38.gif"/><br /> 
CData Content:(1508): <b>Current Conditions:</b><br /> 
CData Content:(1508): Thunder in the Vicinity, 32 C<BR /> 
CData Content:(1508): <BR /><b>Forecast:</b><BR /> 
CData Content:(1508): Tue - Scattered Thunderstorms. High: 31 Low: 25<br /> 
CData Content:(1508): Wed - Scattered Thunderstorms. High: 31 Low: 25<br /> 
CData Content:(1508): Thu - Scattered Thunderstorms. High: 31 Low: 25<br /> 
CData Content:(1508): Fri - Isolated Thunderstorms. High: 31 Low: 25<br /> 
CData Content:(1508): Sat - Scattered Thunderstorms. High: 31 Low: 25<br /> 
CData Content:(1508): <br /> 
CData Content:(1508): <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Singapore__SG/*http://weather.yahoo.com/forecast/SNXX0006_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> 
CData Content:(1508): (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/> 
img url:(1508): http://l.yimg.com/a/i/us/we/52/38.gif ---> image url 
+0

嗨,我想知道我應該在哪裏放? 我是新來的編碼。 謝謝 – Febbie

+0

我怎麼知道cozi不知道你的應用是做什麼的。把它放在類裏 – Raghunandan

+0

嗨,我已經把我的代碼放在上面的 – Febbie