EMF不能载入schema问题的解决

在EMF里可以用XML Schema定义模型,然后转换为genmodel模型,但我这里在生成genmodel向导的第四步按Load后会产生一个NullPointerException如下,导致无法继续:

java.lang.NullPointerException
    at org.apache.crimson.tree.ElementNode2.getAttributeNodeNS(ElementNode2.java:432)
    at org.apache.crimson.tree.ElementNode2.hasAttributeNS(ElementNode2.java:388)
    at org.eclipse.xsd.ecore.XSDEcoreBuilder.getEcoreAttribute(XSDEcoreBuilder.java:2336)
    at org.eclipse.xsd.ecore.XSDEcoreBuilder.getEcoreAttribute(XSDEcoreBuilder.java:2329)
    at org.eclipse.xsd.ecore.XSDEcoreBuilder.getEStructuralFeature(XSDEcoreBuilder.java:2004)
    at org.eclipse.xsd.ecore.XSDEcoreBuilder.generate(XSDEcoreBuilder.java:2117)
    at org.eclipse.xsd.ecore.XSDEcoreBuilder.generate(XSDEcoreBuilder.java:2067)
    at org.eclipse.xsd.ecore.importer.XSDImporter.doComputeEPackages(XSDImporter.java:123)
    at org.eclipse.emf.importer.ModelImporter.computeEPackages(ModelImporter.java:664)
    at org.eclipse.emf.importer.ui.contribution.base.ModelDetailPage.refreshModel(ModelDetailPage.java:488)
    at org.eclipse.emf.importer.ui.contribution.base.ModelDetailPage$2.execute(ModelDetailPage.java:434)
    at org.eclipse.ui.actions.WorkspaceModifyOperation$1.run(WorkspaceModifyOperation.java:98)
    at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:1719)
    at org.eclipse.ui.actions.WorkspaceModifyOperation.run(WorkspaceModifyOperation.java:110)
    at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:346)
    at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:291)
    at org.eclipse.jface.wizard.WizardDialog.run(WizardDialog.java:830)
    at org.eclipse.emf.importer.ui.contribution.base.ModelDetailPage.refreshModel(ModelDetailPage.java:457)
    at org.eclipse.emf.importer.ui.contribution.base.ModelDetailPage.doHandleEvent(ModelDetailPage.java:241)
    at org.eclipse.xsd.ecore.importer.ui.XSDDetailPage.doHandleEvent(XSDDetailPage.java:91)
    at org.eclipse.emf.importer.ui.contribution.base.ModelImporterPage.handleEvent(ModelImporterPage.java:106)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1021)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2867)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2572)
    at org.eclipse.jface.window.Window.runEventLoop(Window.java:809)
    at org.eclipse.jface.window.Window.open(Window.java:787)
    at org.eclipse.ui.actions.NewWizardAction.run(NewWizardAction.java:181)
    at org.eclipse.ui.actions.NewWizardDropDownAction.run(NewWizardDropDownAction.java:174)
    at org.eclipse.jface.action.Action.runWithEvent(Action.java:996)
    at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:538)
    at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:488)
    at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:441)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1021)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2867)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2572)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1699)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1663)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:367)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:143)
    at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:103)
    at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:226)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:376)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:163)
    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:324)
    at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334)
    at org.eclipse.core.launcher.Main.basicRun(Main.java:278)
    at org.eclipse.core.launcher.Main.run(Main.java:973)
    at org.eclipse.core.launcher.Main.main(Main.java:948) 

这是由Sun JDK1.4的一个bug造成的,解决方法是通过参数指定eclipse使用第三方的XML Parser,例如Xerces-J。首先下载Xerces-J并解压缩,然后在eclipse的启动参数后加上“-vmargs -Djava.endorsed.dirs=”即可。

参考资料

EMF关于这个问题的说明:XSD Model Loading: Crimson DOM Bug, Workaround & Download