pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>MyXMLParese</groupId>
<artifactId>MyXMLParese</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description></description>
<dependencies>
<dependency>
<groupId>mycom</groupId>
<artifactId>smproxy</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>fop</groupId>
<artifactId>fop</artifactId>
<version>0.20.5</version>
</dependency>
</dependencies>
</project>
错误提示
Exception in thread "main" java.lang.ClassCastException: org.apache.xerces.dom.DeferredTextImpl
at java.util.XMLUtils.importProperties(XMLUtils.java:97)
at java.util.XMLUtils.load(XMLUtils.java:69)
at java.util.Properties.loadFromXML(Properties.java:701)
at LoadSampleXML.main(LoadSampleXML.java:8)
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Hi</comment>
<entry key="foo">bar</entry>
<entry key="fu">baz</entry>
</properties>
源码
import java.util.*;
import java.io.*;
public class LoadSampleXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("sampleprops.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("/nThe foo property: " + prop.getProperty("foo"));
}
}
分析
fop的依赖pom.xml 有
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.2.1</version>
</dependency>
而在smproxy包里里面也有xerces,,如果将
<dependency>
<groupId>mycom</groupId>
<artifactId>smproxy</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>fop</groupId>
<artifactId>fop</artifactId>
<version>0.20.5</version>
</dependency>
当中任意一个或两个都注悉掉,那程序没任何问题
在引用maven的依赖时,需要注意 依赖包的依赖,有可能会导致程序出错