XPath节点在新行之后

时间:2014-07-10 04:20:05

标签: java xml xpath

我正在尝试获取具有div align center的节点的节点文本: `

                <tr> 
                  <td width="39" class="back1"><b class="texto4">CRN</b></td>
                  <td width="60" class="back1"><b class="texto4">Materia</b></td>
                  <td width="53" class="back1"><b class="texto4">Secci&oacute;n</b></td>
                  <td width="55" class="back1"><b class="texto4">Cr&eacute;ditos</b></td>
                  <td width="156" class="back1"><b class="texto4">T&iacute;tulo</b></td>
                  <td width="69" class="back1"><b class="texto4">Cupo</b></td>
                  <td width="57" class="back1"><b class="texto4">Inscritos</b></td>
                  <td width="77" class="back1"><b class="texto4">Disponible</b></td>
                </tr>
                <tr> 
                  <td width="39"><font class="texto4"> 
                    10110                        </font></td>
                  <td width="60"><font class="texto4"> 
                    IIND1000                        </font></td>
                  <td width="53"><font class="texto4"> 
                  <div align="center">
                    1                        </div></font></td>
                  <td width="55"><font class="texto4"> 
                    <div align="center">
                    3                       </div>
                    </font></td>
                  <td width="156"><font class="texto4"> 
                    INTROD. INGEN. INDUSTRIAL                        </font></td>
                  <td width="69"><font class="texto4"> 
                    100                        </font></td>
                  <td width="57"><font class="texto4"> 
                    100                        </font></td>
                  <td width="77"><font class="texto4"> 
                    0                        </font></td>
                </tr>
              </table> `

问题是JTidy没有得到该子节点中的文本,因为(我假设在这里)<font class="texto4"><div align="center">之间有一个新的行。当我获取List中该节点下的每个元素并打印它时的输出是空的。

到目前为止,我的XPathExpression是expr = xpath.compile("//td[@width='55']/font/div/text()");(或宽度='69',对于另一个节点)。

我的完整代码尝试是:

String cod = "IIND2401";
    String dep = cod.substring(0, 4);
    System.out.println(dep);
    try {
        URL url = new URL("http://registroapps.uniandes.edu.co/scripts/adm_con_horario1_joomla.php?depto="+dep);
        Tidy tidy = new Tidy();
        tidy.setXHTML(true);    
        Document doc = tidy.parseDOM(url.openStream(), System.out);

        // Use XPath to obtain whatever you want from the (X)HTML
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("//td[@width='39']/font/text()");
        NodeList crn = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);

然后我拿出我获得的代码,将它们放入List并打印出来:

List<String> crns = new ArrayList<String>();
        for (int i = 0; i < crn.getLength(); i++) {
            crns.add(crn.item(i).getNodeValue()); 
        }

的System.out.println( “CRNS:”);             的System.out.println(CRNS);

结果是我大学为IIND教员开设的课程ID很长。 CRNS: [10110,16886,12420,12526,12527,12528,13510,15146,12544,12545,(...)] 我无法更改HTML文档,我正试图从我的大学网站上解析它。有什么建议?谢谢你们。

0 个答案:

没有答案