0

我可以知道如何检索 ![CDATA[ for 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; 
    }

}
4

1 回答 1

1

您可以使用 Dom 解析器来解析 decrition 标签,然后在字符串上使用正则表达式来提取 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 
于 2013-07-17T03:22:17.933 回答