【问题标题】:How do I retrieve the image from ![CDATA[ in JAVA(android/eclipse)如何从 ![CDATA[ in JAVA(android/eclipse) 中检索图像
【发布时间】:2013-07-17 02:21:31
【问题描述】:

我可以知道如何检索 ![CDATA[ 用于 Java (android/eclipse) 吗? 我想使用 Eclipse 软件从代码中检索图像并将其显示在 android 应用程序中。

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

}

【问题讨论】:

    标签: android eclipse weather


    【解决方案1】:

    您可以使用 Dom 解析器来解析 decription 标签,然后在字符串上使用正则表达式来提取 url。您也可以使用其他解析器。下面是一个如何使用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 
    

    【讨论】:

    • 我怎么知道 cozi 不知道你的应用是做什么的。把它放在一个类中
    • 你可以创建一个方法并在你想要的地方返回getCharacterDataFromElement(line)的结果。很难说出你想要什么。
    • 我无法让它工作,因为当我将它放在我的代码中时,出现错误
    • 代码没有加载。当我将它粘贴到我的代码中时,它带有下划线。
    猜你喜欢
    • 2013-08-14
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多