昨天从官网上下了个Xstream,运行了一个简单的测试用例
public void testConvertsPropertiesObjectToShortKeyValueElements() {
Properties in = new Properties();
in.setProperty("hello", "world");
in.setProperty("foo", "cheese");
String expectedXML = "" + "<properties>\n"
+ " <property name=\"hello\" value=\"world\"/>\n"
+ " <property name=\"foo\" value=\"cheese\"/>\n"
+ "</properties>";
XStream xstream = new XStream();
String actualXML = xstream.toXML(in);
assertEquals(expectedXML, actualXML);
Properties expectedOut = new Properties();
expectedOut.setProperty("hello", "world");
expectedOut.setProperty("foo", "cheese");
Properties actualOut = (Properties) xstream.fromXML(actualXML);
assertEquals(in, actualOut);
assertEquals(in.toString(), actualOut.toString());
}
一运行,就报错了,错误信息如下:
com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:56)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1011)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1002)
at com.xmljson.test.PropertiesConverterTest.testConvertsPropertiesObjectToShortKeyValueElements(PropertiesConverterTest.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
在初始化Xstream的地方看来下反编译的源码
public XStream()
{
this(null, (Mapper)null, ((HierarchicalStreamDriver) (new XppDriver())));
}
发现Xstream默认是使用XppDriver的
把Xstream的Driver改为
XStream xstream = new XStream(new DomDriver());
测试运行通过,至于为什么要使用DomDriver,还请各位大神指教,谢谢。