xpath无法读取xml
package testdemo1;
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 xPathXml {
public void testXPathXml(String xPathStr) {
String file="d:/eclipse-workspace/xmlTest/src/plan.xml";
SAXReader sax = new SAXReader();
try {
Document dm = sax.read(file);
List<Node> nodes = dm.selectNodes(xPathStr);
System.out.println(nodes.size());
for(Node node : nodes) {
Element course = (Element)node;
System.out.print(course.attributeValue("id"));
System.out.print(course.elementText("course-name"));
System.out.print(course.elementText("class-hour"));
System.out.print(course.elementText("exam-form"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
xPathXml xpathxml = new xPathXml();
xpathxml.testXPathXml("//scoure");
// xpathxml.testXPathXml("//scoure[class-hour<50]");
// xpathxml.testXPathXml("//scoure[course-name='高等数学']");
// xpathxml.testXPathXml("//scoure[@id='001']");
// xpathxml.testXPathXml("//scoure[position<3]");
}
}
------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<teaching-plan>
<course id="001">
<course-name>大学英语</course-name>
<class-hour>36</class-hour>
<exam-form>考试</exam-form>
</course>
<course id="002">
<course-name>高等数学</course-name>
<class-hour>70</class-hour>
<exam-form>考试</exam-form>
</course>
<course id="003">
<course-name>计算机应用基础</course-name>
<class-hour>108</class-hour>
<exam-form>上机考试</exam-form>
</course>
</teaching-plan>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星