为什么设置XPathExp参数没效果 每次都是遍历全部元素

来源:3-8 XPath实验室

C_LANNAD

2020-03-13 18:30:48

public class XPathTest {

public void xpath(String xpathExp) {

String file="C:/Program Files/eclipse-workspace/xml/src/teaching-plan-schema.xml";

SAXReader reader=new SAXReader();

try {

Document document=reader.read(file);

List<Node> nodes=document.selectNodes(xpathExp);

for(Node node:nodes) {

Element plan=(Element)node;

System.out.println(plan.attributeValue("id"));

System.out.println(plan.elementText("course-name"));

System.out.println(plan.elementText("class-hour"));

System.out.println(plan.elementText("exam-form"));

}

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


public static void main(String[] args) {

// TODO Auto-generated method stub

    XPathTest test=new XPathTest();

  //  test.xpath("//course");

  //  test.xpath("//course[class-hour<50]");

   // test.xpath("//course[(course-name)='高等数学']");

   // test.xpath("//course[@id=2]");

    //test.xpath("//course[position()<3]");

    test.xpath("//@id");

}


}

<?xml version="1.0" encoding="UTF-8"?>

<!-- 教学计划 --><teaching-plan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="teaching.xsd">

<course id="1">

<course-name>大学英语</course-name>

<class-hour>36</class-hour>

<exam-form>考试</exam-form>

</course>

<course id="2">

<course-name>高等数学</course-name>

<class-hour>70</class-hour>

<exam-form>考试</exam-form>

</course>

<course id="3">

<course-name>计算机应用基础</course-name>

<class-hour>100</class-hour>

<exam-form>上机考试</exam-form>

</course>

<course id="4">

<course-name>体育课</course-name>

<class-hour>60</class-hour>

<exam-form>跑步测验</exam-form>

</course>

</teaching-plan>


写回答

3回答

好帮手慕阿满

2020-03-14

同学你好,关于项目名上有红色感叹号的,建议同学查看如下Problems中报什么错误,如:

http://img.mukewang.com/climg/5e6c936a09da467c07120204.jpg

祝:学习愉快~

0

C_LANNAD

提问者

2020-03-14

http://img.mukewang.com/climg/5e6c96c00955d28900000000.jpg这个错误提示我之前从阿里云下载的一个jar包 用这个会报错我就从教辅里重新下载的jaxen.jar  当时是直接删除的 现在重新补上好了 刚接触jar包不懂作用和用法 

0

好帮手慕阿满

2020-03-13

同學你好,設置的參數是有效果的,比如:

http://img.mukewang.com/climg/5e6b6edf09cbff8406300283.jpg

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~


0
h_LANNAD
h 我从课程中下载的jaxen.jar文件 移除就会报错 添加到build path中后 我main函数全部清空都能运行出来遍历的结果 eclipse的project列表中这个project前面还有个红色感叹号 不知道到底哪里有问题
h020-03-13
共1条回复

0 学习 · 9666 问题

查看课程