Java解析XML报错: java.lang.ClassNotFoundException: org.jaxen.NamespaceContext
代码
// XML格式
SAXReader reader = new SAXReader();
// 通过read方法读取一个文件 转换成Document对象
Document document ;
try {
document = reader.read(new StringReader(gradeInfoData));
// 获取根元素
if(Objects.nonNull(document)){
Element root = document.getRootElement();
Object totalScoreDetail = root.selectObject("/ProjectName//TotalScoreDetail");
// 获取实操分类得分项
if (Objects.nonNull(totalScoreDetail)) {
Element element = (Element) totalScoreDetail;
resMap.put("GGJScore", element.elementText("GGJScore")); // 钢筋得分
resMap.put("GCLScore", element.elementText("GCLScore")); // 土建得分
}
}
return resMap;
} catch (Exception e) {
log.error("解析XML对象失败", e);
}
错误提示
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/NamespaceContext
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at Parser.parse(Parser.java:15)
at Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: org.jaxen.NamespaceContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 5 more
缺少 jaxen.jar
添加依赖
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
解决
逃避不一定躲得过,面对不一定最难过