Search code examples
emfecore

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


I generated an EMF model form an XML schema and everything looked nice from the first look but the in the EMF runtime instance I only can access the attribute feature with its String "name" but the value "/firstfloor..." is not set in EMF. Any idea why?

Thanks in advance!

Part of the xml looks like this:

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

Part of the xsd schema like this:

<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>

Solution

  • I finally found the answer myself. I has to modify the ecore model and set the aspectvalue tag name to "upper bound: -1". EMF changes the model and instead of a value setter "setValue" I can add a collection as value. A bit strange but it works I can produce the xml above and read it into the EMF model.

    Save EMF instance

    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);
    

    Read into 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);