Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How To Persistence Eobject
How To Persistence Eobject [message #673272] Mon, 23 May 2011 05:06
javen jia is currently offline javen jiaFriend
Messages: 7
Registered: December 2009
Junior Member
I get the xml content with the following code.
code:
EPackage poPackage = EcoreFactory.eINSTANCE.createEPackage();
		EFactory eFactory=poPackage.getEFactoryInstance();

		EClass purchaseOrderClass = EcoreFactory.eINSTANCE.createEClass();
		purchaseOrderClass.setName("PurchaseOrder");
		poPackage.getEClassifiers().add(purchaseOrderClass);

		EClass itemClass = EcoreFactory.eINSTANCE.createEClass();
		itemClass.setName("Item");
		poPackage.getEClassifiers().add(itemClass);
		
		EAttribute nameAttribute =
			  EcoreFactory.eINSTANCE.createEAttribute();
		nameAttribute.setName("name");
		nameAttribute.setEType(EcorePackage.eINSTANCE.getEString());
		itemClass.getEStructuralFeatures().add(nameAttribute);

		EAttribute shipToAttribute =
		  EcoreFactory.eINSTANCE.createEAttribute();
		shipToAttribute.setName("shipTo");
		shipToAttribute.setEType(EcorePackage.eINSTANCE.getEString());
		purchaseOrderClass.getEStructuralFeatures().add(shipToAttribute);
		
		EReference eReference = EcoreFactory.eINSTANCE.createEReference();
		eReference.setName("items");
		eReference.setEType(itemClass);
		eReference.setUpperBound(ETypedElement.UNBOUNDED_MULTIPLICITY);
		eReference.setContainment(true);
		purchaseOrderClass.getEStructuralFeatures().add(eReference);
		
		
		EObject order = eFactory.create((EClass)(poPackage.getEClassifier("PurchaseOrder")));
		order.eSet(order.eClass().getEStructuralFeature("shipTo"), "there");
		EObject item1 = eFactory.create((EClass)(poPackage.getEClassifier("Item")));
		item1.eSet(item1.eClass().getEStructuralFeature("name"), "item1");
		((List)order.eGet(eReference)).add(item1);
		EObject item2 = eFactory.create((EClass)(poPackage.getEClassifier("Item")));
		item2.eSet(item2.eClass().getEStructuralFeature("name"), "item2");
		((List)order.eGet(eReference)).add(item2);
		
		
		final ExtendedMetaData data=new BasicExtendedMetaData(ExtendedMetaData.ANNOTATION_URI,EPackage.Registry.INSTANCE,new HashMap());
		data.setFeatureKind(eReference, ExtendedMetaData.ELEMENT_FEATURE);
		//data.setFeatureKind(nameAttribute, ExtendedMetaData.ELEMENT_FEATURE);
		data.setGroup(eReference, eReference);
		data.setName(purchaseOrderClass, "poo");
	
		Map options=new HashMap();
		options.put(XMIResource.OPTION_EXTENDED_META_DATA, data);
		
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml",new XMIResourceFactoryImpl());
		URI fileURI = URI.createFileURI(new File("d:/temp/mypo.xml").getAbsolutePath());
		Resource poResource = resourceSet.createResource(fileURI);
		poResource.getContents().add(order);
		try {
			poResource.save(options);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

the result Xml is
<?xml version="1.0" encoding="ASCII"?>
<poo xmi:version="2.0" xmlns:xmi="" shipTo="there">
  <items name="item1"/>
  <items name="item2"/>
</poo>


but i need the xml like

<?xml version="1.0" encoding="ASCII"?>
<poo xmi:version="2.0" xmlns:xmi="" shipTo="there">
  <items>
     <item name="item1"/>
      <item name="item2"/>
  </items>
</poo>


Thanks!!!!!!!!!!!!!!!
Previous Topic:[CDO] How to do jvm connection to the same repo as the tcp acceptor/connection
Next Topic:[CDO] distinguish first-time construction from deserialization
Goto Forum:
  


Current Time: Tue Apr 23 10:15:23 GMT 2024

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

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

Back to the top