老师为什么我都xml文档用schema限制后,运行xpath查询结果就不显示???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <? xml version = "1.0" encoding = "UTF-8" ?> <!-- 人力资源管理系统 --> < hr xmlns = "http://www.w3school.com.cn" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.w3school.com.cn hr2.xsd" > < employee id = "e001" > < name >张三</ name > < age >31</ age > < salary >4000</ salary > < department > < dname >会计部</ dname > < address >xx大厦-B103</ address > </ department > </ employee > < employee id = "e002" > < name >李四</ name > < age >41</ age > < salary >5000</ salary > < department > < dname >工程部</ dname > < address >xx大厦-B303</ address > </ department > </ employee > < employee id = "e003" > < name >王五</ name > < age >55</ age > < salary >10000</ salary > < department > < dname >公关部</ dname > < address >xx大厦-A001</ address > </ department > </ employee > </ hr > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.mason.dom4j; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import java.util.List; public class XPathTestor { public static void main(String[] args) { XPathTestor xPathTestor= new XPathTestor(); xPathTestor.xpath( "/hr/employee" ); } public void xpath(String xpathExp) { //传入参数为xPath表达式 String file = "C:\\Users\\Mason\\IdeaProjects\\untitled\\src\\hr.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.elementText( "name" )); System.out.println(emp.elementText( "age" )); System.out.println(emp.elementText( "salary" )); } } catch (Exception e) { e.printStackTrace(); } } } |
运行成功没用报错,但就是不显示结果!!这是为什么呢?
但我后来把xml的Schema限制删了之后↓
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <? xml version = "1.0" encoding = "UTF-8" ?> <!-- 人力资源管理系统 --> < hr > < employee id = "e001" > < name >张三</ name > < age >31</ age > < salary >4000</ salary > < department > < dname >会计部</ dname > < address >xx大厦-B103</ address > </ department > </ employee > < employee id = "e002" > < name >李四</ name > < age >41</ age > < salary >5000</ salary > < department > < dname >工程部</ dname > < address >xx大厦-B303</ address > </ department > </ employee > < employee id = "e003" > < name >王五</ name > < age >55</ age > < salary >10000</ salary > < department > < dname >公关部</ dname > < address >xx大厦-A001</ address > </ department > </ employee > </ hr > |
这样就可以正常显示出xpath后的结果,这是为什么呢?
1
收起
正在回答
3回答
同学你好,同学需要修改一下xml和xsd文件的头部,修改后xml文件的头部如:
1 2 | < hr xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "hr.xsd" > |
xsd文件的头部,如:
1 | < xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema" > |
这样就可以读取schema约束的xml文档了。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
MasonM
2019-03-24 15:13:40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <? xml version = "1.0" encoding = "UTF-8" ?> < xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema" targetNamespace = "http://www.w3school.com.cn" xmlns = "http://www.w3school.com.cn" elementFormDefault = "qualified" > < xs:element name = "hr" > < xs:complexType > < xs:sequence > < xs:element name = "employee" minOccurs = "1" maxOccurs = "10" > < xs:complexType > < xs:sequence > < xs:element name = "name" type = "xs:string" ></ xs:element > < xs:element name = "age" type = "xs:integer" ></ xs:element > < xs:element name = "salary" type = "xs:integer" ></ xs:element > < xs:element name = "department" > < xs:complexType > < xs:sequence > < xs:element name = "dname" type = "xs:string" ></ xs:element > < xs:element name = "address" type = "xs:string" ></ xs:element > </ xs:sequence > </ xs:complexType > </ xs:element > </ xs:sequence > < xs:attribute name = "id" type = "xs:string" use = "required" ></ xs:attribute > </ xs:complexType > </ xs:element > </ xs:sequence > </ xs:complexType > </ xs:element > </ xs:schema > |
这就是xsd约束文件
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧