使用从 XSD 生成的 EMF 模型读取 XML 只会读取属性而不是值

Reading XML with EMF model generated from XSD only reads attributes but not values

我从 XML 模式生成了一个 EMF 模型,乍一看一切都很好,但在 EMF 运行时实例中,我只能使用其字符串 "name" 访问属性功能,但是EMF 中未设置值“/firstfloor...”。知道为什么吗?

提前致谢!

部分 xml 如下所示:

<aspectvalue feature="name">/firstfloor/temperature/CurrentRoomTemp</aspectvalue>

部分 xsd 架构如下:

<xs:complexType name="FeatureListType">
    <xs:sequence>
        <xs:element name="feature" minOccurs="1" maxOccurs="unbounded" type="FeatureType" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="FeatureType">
    <xs:attribute name="name" use="required" type="NameType" />
    <xs:attribute name="optional" use="optional" type="xs:boolean" />
    <xs:attribute name="deprecated" use="optional" type="xs:boolean" />
</xs:complexType>

我终于自己找到了答案。我必须修改 ecore 模型并将 aspectvalue 标签名称设置为 "upper bound: -1"。 EMF 更改了模型,而不是值 setter "setValue" 我可以添加一个集合作为值。有点奇怪,但它有效我可以生成上面的 xml 并将其读入 EMF 模型。

保存 EMF 实例

Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put("xml", new XMLResourceFactoryImpl());

    ResourceSet resSet = new ResourceSetImpl();
    Resource resource = resSet.createResource(URI.createFileURI("/tmp/test.xml"));
    resource.getContents().add(emfModelData);
    resource.save(Collections.EMPTY_MAP);

读入 EMF

EPackage.Registry.INSTANCE.put(LibraryPackage.eNS_URI, LibraryPackage.eINSTANCE);

    // add file extension to registry
    ResourceFactoryRegistryImpl.INSTANCE.getExtensionToFactoryMap().put("xml", new GenericXMLResourceFactoryImpl());

    Resource resource = new XMLResourceImpl(URI.createFileURI("/tmp/test.xml"));
    resource.load(Collections.EMPTY_MAP);
    EObject root = resource.getContents().get(0);
    System.out.println(root);