2011-10-19 44 views
1

如何從java中使用php生成的xml中獲取數據。 我需要的數據顯示在我的Android應用程序的列表視圖。從xml獲取數據到java

該phpcode採取數據形式的mysqlquery,並獲取變量xml中的數組,並將其放回回出。 mysqlquery的數據來自Android應用程序通過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

使用一個XML分析器,如一系列教程[遍佈互聯網](https://www.google.com/search?q=android%20parse%20xml)所示。 –

+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或更高層次的抽象和dom4jJDOM - 你必須做更多的工作,這些,但有一個更多的靈活性。

+0

有兩點:JAXB不能在Android上運行,Simple XML是Android IMO上這項工作的最佳框架。 –

2

有這種事情的XML解析工具。在我的工作中,我們使用XMLBeans: http://xmlbeans.apache.org/

我發現使用它是相當直接的,並且在這類東西中有種級別的業餘愛好者。