我有一个 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)
由于这是直接在我的 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);
架构文件如下所示:
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Inner" type="MyType">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="MyType">
<xs:sequence> </xs:sequence>
</xs:complexType>
</xs:schema>
这是xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Inner>
</Inner>
</Root>
有什么想法吗?任何帮助表示赞赏!