how to load an XML-Instance into Ecore and access its attribute values [message #634216] |
Wed, 20 October 2010 19:34 |
Ben Messages: 2 Registered: October 2010 |
Junior Member |
|
|
Hi,
I am new to EMF/Ecore (and to Eclipse too) and I have small problem.
I convert a XML-Schema to Ecore using this Code:
XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
ResourceSet resourceSet = new ResourceSetImpl();
final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(resourceSet.getPackageRegistry());
resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());
Collection<?> eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI(path_to_xsd_file));
for (Iterator<?> iter = eCorePackages.iterator(); iter.hasNext();) {
EPackage element = (EPackage) iter.next();
resourceSet.getPackageRegistry().put(element.getNsURI(), element);
System.out.println(element.getNsURI());
}
As far as I see, this works beside the fact, that I have a EClass "DocumentRoot" in the Ecore-Model, which isn't modeled in my XML-Schema.
Furthermore I want to load a XML-Instance-File related to this XSD-File. I am trying it with this code:
HashMap<String, Boolean> options = new HashMap<String, Boolean>();
options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createFileURI(path_to_xml_file));
resource.load(options);
Now my problem is the following. I want to access the XML-Data in the previously loaded Model and extract the values of the attributes for each EClass. Is there an easy way to achieve this?
Thanks in advance.
Cheers,
Ben.
|
|
|
Re: how to load an XML-Instance into Ecore and access its attribute values [message #634361 is a reply to message #634216] |
Thu, 21 October 2010 11:53 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
Ben,
It's best to use the eclipse.tools.emf newsgroup/forum for questions
about Ecore core.
More comments below.
Ben wrote:
> Hi,
>
> I am new to EMF/Ecore (and to Eclipse too) and I have small problem.
>
> I convert a XML-Schema to Ecore using this Code:
>
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
> ResourceSet resourceSet = new ResourceSetImpl();
> final ExtendedMetaData extendedMetaData = new
> BasicExtendedMetaData(resourceSet.getPackageRegistry());
> resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED _META_DATA,
> extendedMetaData);
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xml",
> new XMLResourceFactoryImpl());
> Collection<?> eCorePackages =
> xsdEcoreBuilder.generate(URI.createFileURI(path_to_xsd_file) );
Hopefully this path is absolute...
>
> for (Iterator<?> iter = eCorePackages.iterator(); iter.hasNext();) {
> EPackage element = (EPackage) iter.next();
> resourceSet.getPackageRegistry().put(element.getNsURI(), element);
> System.out.println(element.getNsURI());
> }
>
> As far as I see, this works beside the fact, that I have a EClass
> "DocumentRoot" in the Ecore-Model, which isn't modeled in my XML-Schema.
You've read
http://www.theserverside.com/news/1364302/Binding-XML-to-Jav a to
understand that?
>
> Furthermore I want to load a XML-Instance-File related to this
> XSD-File. I am trying it with this code:
>
> HashMap<String, Boolean> options = new HashMap<String, Boolean>();
> options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
> Resource resource =
> resourceSet.createResource(URI.createFileURI(path_to_xml_fil e));
> resource.load(options);
>
>
> Now my problem is the following. I want to access the XML-Data in the
> previously loaded Model and extract the values of the attributes for
> each EClass. Is there an easy way to achieve this?
I'd suggest reading the above article. I think it explains everything
you need to know.
>
> Thanks in advance.
>
> Cheers,
> Ben.
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.03019 seconds