2012-02-21 16 views
0

私は自分のホストに外部のmysqlデータベースを持っており、データベースからレコードを抽出してxml形式にするサーバー側にもphpファイルを作成しています。 (下に表示)xmlを解析してアンドロイドエミュレータのコンテンツを表示するにはどうすればよいですか?

<?php 
mysql_connect("localhost","username","password") or die("Could not  connect to host."); 
mysql_select_db("footieDB") or die("Could not find database."); 
$resultID=mysql_query("select * from league"); //$resultID was $sql 


$xml_output = "<?xml version=\"1.0\"?>\n"; 
$xml_output .= "<leagues>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 
    $row = mysql_fetch_assoc($resultID); 
    $xml_output .= "\t<league>\n"; 
    $xml_output .= "\t\t<leagueID>" . $row['leagueID'] . "</leagueID>\n"; 
    $xml_output .= "\t\t<leagueName>" . $row['leagueName'] . "</leagueName>\n"; 
    $xml_output .= "\t</league>\n"; 
} 

$xml_output .= "</leagues>"; 

echo $xml_output; 

mysql_close(); 
?> 

このXMLを解析してアンドロイドエミュレータに情報を表示するにはどうすればよいですか? 例をお待ちしております。

ありがとうございます。

+0

http://www.google.com.ng/search?sourceid=chrome&ie=UTF-8&q=parse+xml+android – Mob

答えて

1

実際、XMLの代わりにJSON形式でデータを配置するのは間違いです。 XML解析は楽しいものではなく、JSONはすばやく簡単です。あなたが決定した場合:

String example1; 
String example2; 
// establish a connection with the server 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpGet httpget = new HttpGet("http://www........./your_stuff.xml"); 

      // execute the post and get the response 
      HttpResponse response = httpclient.execute(httpget); 

      byte[] responsebody = EntityUtils.toByteArray(response.getEntity()); 
      String result = new String(responsebody, 0, responsebody.length, "UTF-8"); 

      // Go through the XML with an XML pull parser, identifying items 
      // by their tag names 
      final XmlPullParser xpp = Xml.newPullParser(); 
      xpp.setInput(new StringReader(result)); 
      int eventType = xpp.getEventType(); 
      while (eventType != XmlPullParser.END_DOCUMENT) { 
       if(eventType == XmlPullParser.START_TAG) { 
        if (xpp.getName().equalsIgnoreCase("example1")) { 
         eventType = xpp.next(); 
         example1 = xpp.getText(); 
        } else if (xpp.getName().equalsIgnoreCase("example2")) { 
         eventType = xpp.next(); 
         example2 = xpp.getText(); 
        } 
       } 
      eventType = xpp.next(); 
      } 
      ....... etc 
関連する問題