我如何从java中获取xml生成的数据。 我需要数据在我的Android应用程序的列表视图中显示它。
phpcode从mysqlquery获取数据并获取变量xml中的数组并将其放在echo out上。 mysqlquery的数据来自于安卓的Android应用程序。
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>
谢谢!
答案 0 :(得分:2)
有这种东西的xml解析工具。在我的工作中,我们使用XMLBeans: http://xmlbeans.apache.org/
我发现使用它是相当简单的,并且在这类事情上有点像业余爱好者。
答案 1 :(得分: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或XMLBeans用于更高级别的抽象,dom4j或JDOM用于更低级别的抽象 - 您必须对这些做更多工作但有更多的灵活性。