Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » how to load an XML-Instance into Ecore and access its attribute values
how to load an XML-Instance into Ecore and access its attribute values [message #634216] Wed, 20 October 2010 19:34 Go to next message
Ben is currently offline 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 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26129
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.
>
Re: how to load an XML-Instance into Ecore and access its attribute values [message #634439 is a reply to message #634361] Thu, 21 October 2010 17:31 Go to previous message
Ben is currently offline Ben
Messages: 2
Registered: October 2010
Junior Member
Hi,

thanks for your answer. This solved my problem.

Cheers,
Ben
Previous Topic:annotation references
Next Topic:XSD To ECore to XML
Goto Forum:
  


Current Time: Tue Oct 21 13:51:35 GMT 2014

Powered by FUDForum. Page generated in 0.01746 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software