老师,方便检查下吗
来源:3-9 自由编程
流下了没有技术的眼泪
2020-04-01 19:24:55
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Xpathceshi {
public void xpath(String xpathExp) {
// 文件目录xml文件
String file ="D:\\coding\\xml\\src\\plan1.xml";
SAXReader reader =new SAXReader();
try {
Document document = reader.read(file);
List<Node> nodes = document.selectNodes(xpathExp);
for(Node node : nodes){
Element emp =(Element)node;
System.out.println(emp.attributeValue("no"));
System.out.println(emp.elementText("class-name"));
System.out.println(emp.elementText("class-hour"));
System.out.println(emp.elementText("exam-form"));
System.out.println("=============================");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void main(String[] args) {
XPathTestor testor =new XPathTestor();
testor.xpath("//course");
// 查询课时小于50的课程信息
testor.xpath("//class-hour<50");
// 查询课程名为高等数学的课程信息
testor.xpath("//employee[class-name='高等数学']");
// 查询属性id为001的课程信息
testor.xpath("//employee[@class-id=001]");
// 查询前两条课程信息
testor.xpath("//employee[@class-id=001] | //employee[@class-id=002]" );
testor.xpath("//employee[position()<3]");
}
}plan1 <?xml version="1.0" encoding="UTF-8"?> <teaching-plan> <course id="s01"> <course-name>大学英语</course-name> <class-hour>36</class-hour> <exam-form>考试</exam-form> </course> <course id="s02"> <course-name>高等数学</course-name> <class-hour>70</class-hour> <exam-form>考试</exam-form> </course> <course id="s03"> <course-name>计算机应用基础</course-name> <class-hour>108</class-hour> <exam-form>上机考试</exam-form> </course> <course id="s04"> <course-name>体育</course-name> <class-hour>20</class-hour> <exam-form>体育测试</exam-form> </course> </teaching-plan>
3回答
好帮手慕小琪
2020-04-02
同学你好,
1. 同学不用写s和xpathExp,老师用的是idea,会提示要输入内容的类型是什么,这个不是人为输入的,和eclipse有一些不同,同学不需要在意。
2. 同学的代码并没有更改查询语句时的节点错误。改为下图所示:


建议同学把这章关于xpath的内容重新看一下,巩固一下学过的知识。
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
流下了没有技术的眼泪
提问者
2020-04-02
这样呢
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Xpathceshi {
public void xpath(String xpathExp) {
// 文件目录xml文件
String file ="D:\\coding\\xml\\src\\plan1.xml";
SAXReader reader =new SAXReader();
try {
Document document = reader.read(file);
List<Node> nodes = document.selectNodes(xpathExp);
for(Node node : nodes){
Element emp =(Element)node;
System.out.println(emp.attributeValue(s:"id"));
System.out.println(emp.elementText(s:"class-name"));
System.out.println(emp.elementText(s:"class-hour"));
System.out.println(emp.elementText(s:"exam-form"));
System.out.println("=============================");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Xpathceshi testor =new Xpathceshi();
String xpathExp;
testor.xpath(xpathExp:"//course");
// 查询课时小于50的课程信息
testor.xpath(xpathExp:"//class-hour<50");
// 查询课程名为高等数学的课程信息
testor.xpath(xpathExp:"//employee[class-name='高等数学']");
// 查询属性id为001的课程信息
testor.xpath(xpathExp:"//employee[@class-id=s01]");
// 查询前两条课程信息
testor.xpath("xpathExp://employee[position()<3]");
}
}
好帮手慕小琪
2020-04-02
同学你好,同学的问题老师将一一解答:
1. 类名为Xpathceshi,则主方法实例化应为如图所示:

2. 同学的节点值书写有误,应对照path.xml的节点来进行书写。修改后代码如下:

3. 主方法书写有误,同学代码如下:

修改后代码如下:

4. 查询课程小于50的课程信息及之后的查询语句均有误,同学代码如下:

修改后代码如下:

建议同学把这章关于xpath的内容重新看一下,巩固一下学过的知识,加油,多看几遍好处多多。
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题