为什么设置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回答
同学你好,关于项目名上有红色感叹号的,建议同学查看如下Problems中报什么错误,如:

祝:学习愉快~
C_LANNAD
提问者
2020-03-14
这个错误提示我之前从阿里云下载的一个jar包 用这个会报错我就从教辅里重新下载的jaxen.jar 当时是直接删除的 现在重新补上好了 刚接触jar包不懂作用和用法
好帮手慕阿满
2020-03-13
同學你好,設置的參數是有效果的,比如:

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