Вопрос

may I know how to retrieve the ![CDATA[ for Java (android/eclipse)? I would like to retrieve the image from the the code and display it in android apps, using the Eclipse software.

The MainActivity.java is my code. Advice/help needed.

Thank you

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

}
Это было полезно?

Решение

You can use Dom parser to parse the decription tag and then ue regex on the string to extract url. You can use other parsers also. The below is an example of how to use dom and retrieve the image 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));
}

The 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 Output

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 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top