我有一个调用此方法的代码。解组时收到ClassCastException。创建JAXBContext对象时,我传递了正确的类名。

public TestsuiteType parseTestSuiteXML(String path) throws JAXBException, FileNotFoundException {
        JAXBContext context = JAXBContext.newInstance(TestsuiteType.class);
        Unmarshaller um = context.createUnmarshaller();
        TestsuiteType ts = (TestsuiteType) um.unmarshal(new FileReader("/Users/admin/Documents/workspace_Nishant_dev/Practice/xmls/TEST-junit.xml"));
        List<TestcaseType> tcs = ts.getTestcase();
        Iterator<TestcaseType> it = tcs.iterator();
        while(it.hasNext()) {
            TestcaseType tc = it.next();
            if(tc.getContent().size() == 0) {
                tcs.remove(tc);
            }
        }
        return ts;
    }


上面的代码的异常跟踪:

Exception in thread "main" java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to com.jaxb.testjunit.TestsuiteType
    at com.jaxb.testing.FailureProcessor.parseTestSuiteXML(FailureProcessor.java:49)
    at com.jaxb.testing.FailureProcessor.main(FailureProcessor.java:24)

最佳答案

您得到一个JAXBElement<TestSuiteType>,而不是TestSuiteType。您的值在jaxbElement.getValue()中。

08-26 15:32