1

如何从 XML/HTML 页面获取数据?

我想制作一个 android 应用程序,用户可以在其中收到关于学校课程失败的通知。这是网站的样子: http ://www.anony.ws/image/pC9

用户在安装应用程序后填写他们的学校班级(klas)。我想从表中选择数据,如果是他们的班级,用户应该收到通知。(例子:

if ( userClass == klas ), then alert ("hey, " + was + " will be " + was + "because of" + opmerking); 

类似的东西。但我不知道我应该如何从表格中挑选数据,这是一个奇怪的页面。使它变得更加困难,因为页面从 (1/2) 淡化到 (2/2) (每页最多 10 个列表,所以 50 个列表 = 5 页),在此处查找示例:http: //suse.ghlyceum.nl /dagrooster/lwoo/。我应该如何选择数据并将它们放入 java vars 中!

我需要挑选的代码示例

<tr class='list odd'>
<td class="list" align="center">6V</td>
<td class="list" align="center">5</td>
<td class="list" align="center">fatl1</td>
<td class="list" align="center">fatl1</td>
<td class="list" align="center">MPs</td>
<td class="list" align="center">MPs</td>
<td class="list" align="center">D03</td>
<td class="list" align="center">lokaal wissel</td>

完整的网站代码:

<html>
<head>
<title>Roosterwijzigingen Locatie Rijnwoude</title>
<meta http-equiv="expires" content="0">
<style type="text/css">
<!--

/* page title */
div.mon_title 
{   
    font-weight: bold;
    font-size: larger;  
}

table.info
{
    background-color: #FFB515;
    border: 2px;
    border-style:solid;
}

table.mon_list
{
    background-color: #FFB515;
    border: 2px;
    border-style:solid;
}

td.info
{
    font-size: 15px;
    font-style: normal;
    font-weight: 100;
 }

th.list,
td.list
{
    background-color: #FFB515;
    font-size: 19pt;
    font-style: normal;
    font-weight: 100;
    border: 2px;
    border-style:solid;
 }

-->
</style>
<meta name="generator" content="Untis 2013">
<meta name="company" content="Gruber &amp; Petters Software, A-2000 Stockerau, Austria, www.grupet.at">
</head>

<body bgcolor="#FEE9B1">
<CENTER>
<TABLE  align="bottom" cellspacing="3" cellpadding="2">
<TR>
<TD align="bottom">
<font face="Verdana" size="6" color="#911083">Roosterwijzigingen</font>
<br><font size="4" color="#000000" face="Arial">Vestiging Rijnwoude</font>
</TD>

<TD>&nbsp;</TD>

<TD>

<TABLE width="100%" cellspacing="2" cellpadding="2">
 <tr>
  <TD></TD>
  <TD>   </TD>
  <TD></TD>
  <TD>   </TD>
  <TD></TD>
 </TR>
 <TR> 
  <TD></TD>
  <TD>   </TD>
  <TD></TD>
  <TD>   </TD>
  <TD><font size="-2">Gewijzigd 16-5-2013 13:54</size></TD>
 </TR>
</TABLE>

</TD>

</tr>
</TABLE>
<p>

<font size="4" face="Arial">
<div class="mon_title">18-05-2013 Maandag</div>
<table class="mon_list" >
    <tr class='list'>
        <th class="list" align="center">Klas</th>
        <th class="list" align="center">uur</th>
        <th class="list" align="center">was</th>
        <th class="list" align="center">wordt</th>
        <th class="list" align="center">was</th>
        <th class="list" align="center">wordt</th>
        <th class="list" align="center">Vaklokaal</th>
        <th class="list" align="center">opmerking</th>
    </tr>



<tr class='list odd'>
    <td class="list" align="center" style="background-color: #6f95d5">4H</td>
    <td class="list" align="center" style="background-color: #6f95d5">5-6</td>
    <td class="list" align="center" style="background-color: #6f95d5">nat1</td>
    <td class="list" align="center" style="background-color: #6f95d5">---</td>
    <td class="list" align="center" style="background-color: #6f95d5">MEn</td>
    <td class="list" align="center" style="background-color: #6f95d5">---</td>
    <td class="list" align="center" style="background-color: #6f95d5">---</td>
    <td class="list" align="center" style="background-color: #6f95d5">aulauur</td>
</tr>

<tr class='list even'>
    <td class="list" align="center">4V</td>
    <td class="list" align="center">1</td>
    <td class="list" align="center">econ3</td>
    <td class="list" align="center">econ3</td>
    <td class="list" align="center">SVn</td></td>
    <td class="list" align="center">SVn</td>
    <td class="list" align="center">310</td>
    <td class="list" align="center">lokaal wissel</td>

</tr>

<tr class='list odd'>
    <td class="list" align="center">6V</td>
    <td class="list" align="center">5</td>
    <td class="list" align="center">fatl1</td>
    <td class="list" align="center">fatl1</td>
    <td class="list" align="center">MPs</td>
    <td class="list" align="center">MPs</td>
    <td class="list" align="center">D03</td>
    <td class="list" align="center">lokaal wissel</td>

</tr>

<tr class='list even'>
    <td class="list" align="center" style="background-color: #6f95d5">6V</td>
    <td class="list" align="center" style="background-color: #6f95d5">8</td>
    <td class="list" align="center" style="background-color: #6f95d5">gs</td>
    <td class="list" align="center" style="background-color: #6f95d5">---</td>
    <td class="list" align="center" style="background-color: #6f95d5">NBa</td>
    <td class="list" align="center" style="background-color: #6f95d5">---</td>
    <td class="list" align="center" style="background-color: #6f95d5">---</td>
    <td class="list" align="center" style="background-color: #6f95d5">aulauur</td>
</tr>

<tr class='list odd'>
    <td class="list" align="center">6V</td>
    <td class="list" align="center">7</td>
    <td class="list" align="center">nat7</td>
    <td class="list" align="center">nat7</td>
    <td class="list" align="center">MEn</td>
    <td class="list" align="center">JHo</td>
    <td class="list" align="center">001</td>
    <td class="list" align="center">het 6e of 8e uur</td>
</tr>


</table>
<p>
<font size="3" face="Arial">
Groene Hart Rijnwoude
</font></font>

</CENTER>
<p><center><font face="Arial" size="2"><a href="http://www.grupet.at" target="_blank" >Untis roostersoftware</a></font></center>
</body>
</html>
4

3 回答 3

0

仅对格式良好的 HTML 文档使用 XML 解析器。对于解析 HTML 文档,例如您发布的文档(似乎格式不正确),建议使用JSoup。阅读食谱介绍,几分钟后你应该会很好。

于 2013-05-21T09:12:29.217 回答
0

页面输出为 HTML。所以,我的偏好是使用JSOUP。它是标准的、简单的,并且可以穿透你给它的每一段 HTML。

在您的 android 项目中包含 JSOUP 库。它们的大小相对适中,值得付出努力,因为您的应用程序似乎要读取大量 html。

public static void main(String[] args) throws Exception {
    // Get The Site and Parse it
    Document doc = Jsoup.connect("http://suse.ghlyceum.nl/dagrooster/lwoo/").get();
    // Select Table
    Element table = document.select(".mon_list").first();

    Iterator<Element> lines = table.select("tr").iterator();
    while (lines.hasNext()) {
        Element line = lines.next();
        System.out.println("TD text : "+line.text());
    }
}

这只是java代码。因此,您可以将它与现有的 Android 代码结合使用来填充列表元素(而不是像代码中显示的那样输出到控制台)并在视图中显示它。

于 2013-05-21T09:16:25.047 回答
0
         URL url = new URL("http://facebook.com");
         in = new BufferedReader(new InputStreamReader(url.openStream()));
         String inputLine;
         while ((inputLine = in.readLine()) != null)
           {
             System.out.println(inputLine);
           }

应该足以让您入门。

于 2013-05-21T09:21:05.907 回答