2011-10-19 9 views
1

javaでphpで生成されたXMLからデータを取得するにはどうすればよいですか? アンドロイドアプリでリストビューに表示するにはデータが必要です。xmlからjavaにデータを取得する

phpcodeは、mysqlqueryからデータを取り出し、variabel xmlの配列をフェッチしてエコーアウトします。 mysqlqueryのデータは、POSTのアンドロイドアプリからのものです。

phpcode:

//MySQL zugangsdaten 
$server = "server"; 
$datenbank = "database"; 
$username = "username"; 
$passwort = "password"; 

//Verbindung zur MySqldatenbank herstellen 
$link = mysql_connect($server, $username, $passwort); 
if (!$link) die(mysql_error()); 

//Datenbank auswählen 
$db = mysql_select_db($datenbank, $link); 
//<---- End Login ----> 

$_linie = htmlspecialchars(mysql_real_escape_string($_POST["linie"]), ENT_COMPAT); 
$_richtung = htmlspecialchars(mysql_real_escape_string($_POST["richtung"]), ENT_COMPAT); 

$sql_befehl = "SELECT * From Kontrolleure where linie = '$_linie' AND richtung = '$_richtung'"; 
$query = mysql_query($sql_befehl, $link); 
if(mysql_error()) 
      { 
       die(mysql_error()); 
      } 

while($result = mysql_fetch_array($query, MYSQL_ASSOC)) 
     { 
      $count = $count + 1; 
      $xml = $xml."<Konduktor>"; 
      $xml = $xml."<id>".$result['id']."</id>"; 
      $xml = $xml."<linie>".$result['linie']."</linie>"; 
      $xml = $xml."<endstation>".$result['richtung']."</endstation>"; 
      $xml = $xml."<station>".$result['station']."</station>"; 
      $xml = $xml."<zeit>".$result['zeit']."</zeit>"; 
      $xml = $xml."</Konduktor>"; 
     } 
echo "<Konduktors count=\"$count\">"; 
echo $xml; 
echo "</Konduktors>"; 

XMLレスポンスは次のようになります。

<Konduktors count="3"> 
    <Konduktor> 
     <id>29</id> 
     <linie>S23</linie> 
     <endstation>Langenthal</endstation> 
     <station>Brugg AG</station> 
     <zeit>17:36:34</zeit> 
    </Konduktor> 
    <Konduktor> 
     <id>30</id> 
     <linie>S23</linie> 
     <endstation>Langenthal</endstation> 
     <station>Lupfig</station> 
     <zeit>17:37:12</zeit> 
    </Konduktor> 
    <Konduktor> 
     <id>32</id> 
     <linie>S23</linie> 
     <endstation>Langenthal</endstation> 
     <station>Birr</station> 
     <zeit>16:23:30</zeit> 
    </Konduktor> 
</Konduktors> 

ありがとうございました!

+2

チュートリアルのロード[インターネット全体](https://www.google.com/search?q=android%20parse%20xml)に示すように、XMLパーサーを使用します。 –

+0

[XPath](http://download.oracle.com/javase/1,5.0/docs/api/javax/xml/xpath/package-summary.html)を使用できます。 – CoolBeans

答えて

1

JAXBは簡単にそのハンドルになります。

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import java.io.StringReader; 
import java.util.ArrayList; 
import java.util.List; 

public class JaxbExample { 
    public static void main(String[] args) throws JAXBException { 
     String xml = 
       "<Konduktors count=\"3\">\n" + 
       " <Konduktor>\n" + 
       "  <id>29</id>\n" + 
       "  <linie>S23</linie>\n" + 
       "  <endstation>Langenthal</endstation>\n" + 
       "  <station>Brugg AG</station>\n" + 
       "  <zeit>17:36:34</zeit>\n" + 
       " </Konduktor>\n" + 
       " <Konduktor>\n" + 
       "  <id>30</id>\n" + 
       "  <linie>S23</linie>\n" + 
       "  <endstation>Langenthal</endstation>\n" + 
       "  <station>Lupfig</station>\n" + 
       "  <zeit>17:37:12</zeit>\n" + 
       " </Konduktor>\n" + 
       " <Konduktor>\n" + 
       "  <id>32</id>\n" + 
       "  <linie>S23</linie>\n" + 
       "  <endstation>Langenthal</endstation>\n" + 
       "  <station>Birr</station>\n" + 
       "  <zeit>16:23:30</zeit>\n" + 
       " </Konduktor>\n" + 
       "</Konduktors>"; 
     Object object = JAXBContext.newInstance(Konduktors.class).createUnmarshaller().unmarshal(new StringReader(xml)); 
     System.out.println(object); 
    } 

    @XmlRootElement(name = "Konduktors") 
    static class Konduktors { 
     private List<Konductor> konductors = new ArrayList<Konductor>(); 

     @XmlElement(name = "Konduktor") 
     public List<Konductor> getKonductors() { 
      return konductors; 
     } 

     public void setKonductors(List<Konductor> konductors) { 
      this.konductors = konductors; 
     } 

     @Override 
     public String toString() { 
      return "Konductors{" + 
        "konductors=" + konductors + 
        '}'; 
     } 
    } 

    static class Konductor { 
     private int id; 
     private String linie; 
     private String endstation; 
     private String zeit; 

     public int getId() { 
      return id; 
     } 

     public void setId(int id) { 
      this.id = id; 
     } 

     public String getLinie() { 
      return linie; 
     } 

     public void setLinie(String linie) { 
      this.linie = linie; 
     } 

     public String getEndstation() { 
      return endstation; 
     } 

     public void setEndstation(String endstation) { 
      this.endstation = endstation; 
     } 

     public String getZeit() { 
      return zeit; 
     } 

     public void setZeit(String zeit) { 
      this.zeit = zeit; 
     } 

     @Override 
     public String toString() { 
      return "Konductor{" + 
        "id=" + id + 
        ", linie='" + linie + '\'' + 
        ", endstation='" + endstation + '\'' + 
        ", zeit='" + zeit + '\'' + 
        '}'; 
     } 
    } 
} 

その他のオプションは、下位レベルのもののためXStreamまたはより高いレベルの抽象化のためのとdom4jまたはJDOMが含まれる - あなたはこれらをより多くの仕事をしなければならないが、持っていますより多くの柔軟性。

+0

2点:JAXBはAndroidでは実行されず、Simple XMLはAndroid IMOでこの仕事に最適なフレームワークです。 –

2

この種のものにはxmlの解析ツールがあります。私の仕事では、XMLBeansを使用しています: http://xmlbeans.apache.org/

私はそれを使ってかなり簡単で、この種のものでランクアマチュアの一種です。

関連する問題