Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Create empty XML from XSD generated Epackage
Create empty XML from XSD generated Epackage [message #1269038] Tue, 11 March 2014 09:50 Go to next message
Nickie woo is currently offline Nickie wooFriend
Messages: 21
Registered: February 2014
Junior Member
Hi

I generated an in-memory EPackage from an .xsd schema and now i want to create an .xml resource from that schema with correct xsi:noNamespaceSchemaLocation for external validation without success. Anyone know a good way to do this ?

This is how i load the schema and register it
ResourceSet resourceSet = new ResourceSetImpl();
XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
		Collection<?> eCorePackages = xsdEcoreBuilder
				.generate(schemaPlatformUri);
		for (Iterator<?> iter = eCorePackages.iterator(); iter.hasNext();) {
			EPackage element = (EPackage) iter.next();
			resourceSet.getPackageRegistry().put(element.getNsURI(), element);
			Registry er = EPackage.Registry.INSTANCE;
			er.put(element.getNsURI(), element);
		}


Then i create the xml with the same name in the same folder and save it
final HashMap<String, Object> options = new HashMap<String, Object>();
		URI xmlUri = schemaPlatformUri.trimFileExtension()
				.appendFileExtension("xml");
		Resource xmlResource = resourceSet.createResource(xmlUri);
                xmlResource.getContents().add(createInitialModel(resourceSet));

        try {
	  xmlResource.save(options);
	} catch (IOException e) {
	  e.printStackTrace();
	}


This is how i initialize the initial values of the xml
protected EObject createInitialModel(ResourceSet resourceSet) {
		EPackage ePackage = getEpackage(resourceSet);
		EClass eClass = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
		System.out.println(eClass.getEAllStructuralFeatures());
		EStructuralFeature eStructuralFeature = eClass
				.getEStructuralFeature(ePackage.getNsPrefix().substring(0, 1)
						.toLowerCase()
						+ ePackage.getNsPrefix().substring(1));
		EObject rootObject = ePackage.getEFactoryInstance().create(eClass);
		rootObject.eSet(eStructuralFeature,
				EcoreUtil.create((EClass) eStructuralFeature.getEType()));
		return rootObject;
	}


This is the result
<?xml version="1.0" encoding="UTF-8"?>
<Apple/>


But i want something like
<?xml version="1.0" encoding="UTF-8"?>
<Apple xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Apple.xsd">
</Apple>


Why isnt xsi:noNamespaceSchemaLocation="Apple.xsd" information getting serialized when an Eobject produced by this Epackage is added to the Xml ?

[Updated on: Tue, 11 March 2014 09:55]

Report message to a moderator

Re: Create empty XML from XSD generated Epackage [message #1269069 is a reply to message #1269038] Tue, 11 March 2014 10:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Nickie,

Comments below.

On 11/03/2014 10:50 AM, Nickie woo wrote:
> Hi
>
> I generated an in-memory EPackage from an .xsd schema and now i want
> to create an .xml resource from that schema with correct
> schemalocation for validation without success. Anyone know a good way
> to do this ?
>
> This is how i load the schema and register it
>
> ResourceSet resourceSet = new ResourceSetImpl();
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
> Collection<?> eCorePackages = xsdEcoreBuilder
> .generate(schemaPlatformUri);
> for (Iterator<?> iter = eCorePackages.iterator();
> iter.hasNext();) {
> EPackage element = (EPackage) iter.next();
> resourceSet.getPackageRegistry().put(element.getNsURI(), element);
> Registry er = EPackage.Registry.INSTANCE;
> er.put(element.getNsURI(), element);
> }
>
> Then i create the xml with the same name in the same folder and save it
> final HashMap<String, Object> options = new HashMap<String, Object>();
> URI xmlUri = schemaPlatformUri.trimFileExtension()
> .appendFileExtension("xml");
> Resource xmlResource = resourceSet.createResource(xmlUri);
> xmlResource.getContents().add(
> createInitialModel(resourceSet));
>
>
> This is how i initialize the initial values of the xml
> protected EObject createInitialModel(ResourceSet resourceSet) {
> EPackage ePackage = getEpackage(resourceSet);
> EClass eClass =
> ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
> System.out.println(eClass.getEAllStructuralFeatures());
> EStructuralFeature eStructuralFeature = eClass
> .getEStructuralFeature(ePackage.getNsPrefix().substring(0, 1)
> .toLowerCase()
> + ePackage.getNsPrefix().substring(1));
> EObject rootObject =
> ePackage.getEFactoryInstance().create(eClass);
> rootObject.eSet(eStructuralFeature,
> EcoreUtil.create((EClass)
> eStructuralFeature.getEType()));
> return rootObject;
> }
>
> This is the result
> <?xml version="1.0" encoding="UTF-8"?>
> <Apple/>
>
> But i want something like <?xml version="1.0" encoding="UTF-8"?>
> <Apple xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="Apple.xsd">
> </Apple>
>
> Why isnt xsi:noNamespaceSchemaLocation="Apple.xsd" information getting
> serialized when an Eobject produced by this Epackage is added to the
> Xml ?
You could have a look at how
org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceImpl does this. You
might want to have a look at what happens in
org.eclipse.emf.ecore.xmi.impl.XMLSaveImpl.addNamespaceDeclarations().
Also keep in mind that the DocumentRoot has a getXSISchemaLocation
feature that you can populate directly as well...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create empty XML from XSD generated Epackage [message #1269110 is a reply to message #1269069] Tue, 11 March 2014 11:52 Go to previous message
Nickie woo is currently offline Nickie wooFriend
Messages: 21
Registered: February 2014
Junior Member
GenericXMLResourceImpl does exactly the same thing as XMLResourceImpl with a few options tags such as OPTION_EXTENDED_META_DATA to true, XMLResource.OPTION_ENCODING to "utf-8" among other things but i cant find anything else there.

I dont generate any Java classes so there is no "DocumentRoot" class with getXSISchemaLocation if thats what you mean ?

All i have right now is an Eobject for the root node and right now i´m not sure how to set the xsi:schemalocation/xsi:nonamespaceschemalocation to some desired value.

I also notice that the generated EPackage have a DocumentRoot Eclass with the following contents

Quote:

0..* xSISchemaLocation : EStringToStringMapEntry
(:.) EStringToStringMapEntry
ExtendedMetaData
kind -> attribute
name -> xsi:schemalocation


But i´m not sure how to use this information to modify my DocumentRoot EObject instance

[Updated on: Tue, 11 March 2014 13:14]

Report message to a moderator

Previous Topic:Having a reference specify itself for the EOpposite
Next Topic:CDOQuery to CDOObject - access to cdo_id
Goto Forum:
  


Current Time: Thu Apr 25 00:02:52 GMT 2024

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

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

Back to the top