Create empty XML from XSD generated Epackage [message #1269038] |
Tue, 11 March 2014 09:50 |
Nickie woo 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 #1269110 is a reply to message #1269069] |
Tue, 11 March 2014 11:52 |
Nickie woo 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
|
|
|
Powered by
FUDForum. Page generated in 0.05734 seconds