加载没有给定名称空间的XML作为EMF模型:“未找到带有uri'null'的软件包”-异常

Mar*_*hen 5 java eclipse-emf

我有一个XSD文件,将其转换为ecore模型并从中生成模型代码。现在,我想为该架构加载一个xml文件,但是继续出现错误:

org.eclipse.emf.ecore.xmi.PackageNotFoundException: 
Package with uri 'null' not found.
(file:/C:/Users/mboeschen/safety/devel/eclipse_plugins...
/de.offis.etas.load/examples/minimal.xml, 2, 7)
Run Code Online (Sandbox Code Playgroud)

因为这是直接在我的xml文件中的根标记之后,所以我怀疑在读取根标记后出现了问题。

我的代码如下:

public static void main(String[] args) throws IOException {

    MinimalPackage.eINSTANCE.eClass();  
    MinimalPackage packageInstance = MinimalPackage.eINSTANCE;
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put("*", new XMLResourceFactoryImpl());

    // Obtain a new resource set
    ResourceSet resSet = new ResourceSetImpl();
    resSet.setResourceFactoryRegistry(reg);

    resSet.getPackageRegistry().put(MinimalPackage.eNS_URI,
            MinimalPackage.eINSTANCE);
    resSet.getPackageRegistry().put(null,
            MinimalPackage.eINSTANCE);

    // Get the resource
    URI uri = URI
    .createFileURI("C:/Users/mboeschen/safety/devel/eclipse_plugins...
                    /de.offis.etas.load/examples/minimal.xml");
    Resource resource = resSet.getResource(uri, true);
    RootType r = (RootType) resource.getContents().get(0);

    System.out.println(r);
Run Code Online (Sandbox Code Playgroud)

模式文件如下所示:

org.eclipse.emf.ecore.xmi.PackageNotFoundException: 
Package with uri 'null' not found.
(file:/C:/Users/mboeschen/safety/devel/eclipse_plugins...
/de.offis.etas.load/examples/minimal.xml, 2, 7)
Run Code Online (Sandbox Code Playgroud)

这是xml文件:

public static void main(String[] args) throws IOException {

    MinimalPackage.eINSTANCE.eClass();  
    MinimalPackage packageInstance = MinimalPackage.eINSTANCE;
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put("*", new XMLResourceFactoryImpl());

    // Obtain a new resource set
    ResourceSet resSet = new ResourceSetImpl();
    resSet.setResourceFactoryRegistry(reg);

    resSet.getPackageRegistry().put(MinimalPackage.eNS_URI,
            MinimalPackage.eINSTANCE);
    resSet.getPackageRegistry().put(null,
            MinimalPackage.eINSTANCE);

    // Get the resource
    URI uri = URI
    .createFileURI("C:/Users/mboeschen/safety/devel/eclipse_plugins...
                    /de.offis.etas.load/examples/minimal.xml");
    Resource resource = resSet.getResource(uri, true);
    RootType r = (RootType) resource.getContents().get(0);

    System.out.println(r);
Run Code Online (Sandbox Code Playgroud)

任何想法在这里发生了什么?任何帮助表示赞赏!

小智 2

当您生成模型时,您可能会得到一个生成的 [YourModelName]ResourceFactoryImpl 类。我的 KML 编辑器也遇到了同样的问题,这就是我解决它的方法:

    public class OgckmlResourceFactoryImpl extends ResourceFactoryImpl {
            ...
        /**
         * Creates an instance of the resource.
         * <!-- begin-user-doc -->
         * <!-- end-user-doc -->
         * @generated NOT
         */
        @Override
        public Resource createResource(URI uri) {
            XMLResource result = new OgckmlResourceImpl(uri);
            setupOptions(result);
            return result;
        }

        protected void setupOptions(XMLResource result)
        {
            ...
            result.getDefaultLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, new BasicExtendedMetaData()
            {
                private static final String NAMESPACE_GOOGLE_EXT_2_2 = "http://www.google.com/kml/ext/2.2";
                private static final String NAMESPACE_OPENGIS_2_2 = "http://www.opengis.net/kml/2.2";
                private static final String NAMESPACE_DEFAULT = NAMESPACE_OPENGIS_2_2;

                @Override
                public EPackage getPackage(String namespace) {
                    if(namespace==null){
                        namespace = NAMESPACE_DEFAULT;
                    }
                    return super.getPackage(namespace);
                }

                @Override
                public EStructuralFeature getElement(String namespace, String name)
                {
                    if (feature == null)
                        feature = super.getElement(NAMESPACE_OPENGIS_2_2, name);
                    if (feature == null)
                        feature = super.getElement(NAMESPACE_GOOGLE_EXT_2_2, name);
                    return feature;
                }
        });
...
    }
    }
Run Code Online (Sandbox Code Playgroud)