【问题标题】:Reading XML with EMF model generated from XSD only reads attributes but not values使用从 XSD 生成的 EMF 模型读取 XML 只读取属性而不读取值
【发布时间】:2015-07-20 04:23:01
【问题描述】:

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

提前致谢!

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

【问题讨论】:

    标签: emf ecore


    【解决方案1】:

    我终于自己找到了答案。我必须修改 ecore 模型并将 aspectvalue 标签名称设置为“上限:-1”。 EMF 更改模型,而不是值设置器“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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2022-01-13
      • 2011-06-21
      • 2010-10-30
      相关资源
      最近更新 更多