Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Modeling (top-level project) » Saving EObject to namespaceless XML file
Saving EObject to namespaceless XML file [message #380550] Fri, 11 May 2007 15:18 Go to next message
Marko Tomljenovic is currently offline Marko TomljenovicFriend
Messages: 62
Registered: July 2009
Member
Hello folks,
does anybody have an idea how to save an EObject to an xml file. The
important thing here is, that the xml file must not have any namespace
delarations/usages.

Thanks in advance

Greetings Marko
Re: Saving EObject to namespaceless XML file [message #380554 is a reply to message #380550] Fri, 11 May 2007 16:24 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030602050407040200050807
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Marko,

Please use the EMF newsgroup for questions about EMF itself. I've added
it to the "to" list on the reply.

Have you looked at the generated XyzExample that's produced in the
*.tests project when you do "Generate Test Code"? Should the package
for this EObject be marked as a non-namespace package (using the
annotation for null namespace schemas described in
http://www.eclipse.org/modeling/emf/docs/overviews/XMLSchema ToEcoreMapping.pdf)
or does it normally have a namespace? If the later, then using the
support added in https://bugs.eclipse.org/bugs/show_bug.cgi?id=166127
here's a way to save and load without a namespace:

public static void main(String[] args)
{
// Create a resource set to hold the resources.
//
ResourceSet resourceSet = new ResourceSetImpl();

// Register the appropriate resource factory to handle all file
extentions.
//

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new LibraryResourceFactoryImpl()
{
@Override
public Resource createResource(URI uri)
{
XMLResource result = (XMLResource)super.createResource(uri);
ExtendedMetaData extendedMetaData =
new BasicExtendedMetaData()
{
@Override
public String getNamespace(EPackage ePackage)
{
return ePackage == LibraryPackage.eINSTANCE ? null
: super.getNamespace(ePackage);
}

@Override
protected boolean isFeatureNamespaceMatchingLax()
{
return true;
}
};
extendedMetaData.putPackage(null, LibraryPackage.eINSTANCE);

result.getDefaultLoadOptions().put(XMLResource.OPTION_EXTEND ED_META_DATA,
extendedMetaData);

result.getDefaultSaveOptions().put(XMLResource.OPTION_EXTEND ED_META_DATA,
extendedMetaData);

result.getDefaultSaveOptions().remove(XMLResource.OPTION_SCH EMA_LOCATION);
return result;
}
});

// Register the package to ensure it is available during loading.
//
resourceSet.getPackageRegistry().put
(LibraryPackage.eNS_URI,
LibraryPackage.eINSTANCE);

// If there are no arguments, emit an appropriate usage message.
//
if (args.length == 0)
{
System.out.println("Enter a list of file paths or URIs that
have content like this:");
try
{
Resource resource =
resourceSet.createResource(URI.createURI("http:///My.library"));
DocumentRoot documentRoot =
LibraryFactory.eINSTANCE.createDocumentRoot();
Library root = LibraryFactory.eINSTANCE.createLibrary();
documentRoot.setLibrary(root);
resource.getContents().add(documentRoot);
resource.save(System.out, null);
System.out.println();
ByteArrayOutputStream out = new ByteArrayOutputStream();
resource.save(out, null);
Resource resource2 =
resourceSet.createResource(URI.createURI("http:///My2.library"));
resource2.load(new ByteArrayInputStream(out.toByteArray()),
null);
resource2.save(System.out, null);
}
catch (IOException exception)
{
exception.printStackTrace();
}
}


Marko T. wrote:
> Hello folks,
> does anybody have an idea how to save an EObject to an xml file. The
> important thing here is, that the xml file must not have any namespace
> delarations/usages.
>
> Thanks in advance
>
> Greetings Marko
>


--------------030602050407040200050807
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Marko,<br>
<br>
Please use the EMF newsgroup for questions about EMF itself.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Saving EObject to namespaceless XML file [message #592573 is a reply to message #380550] Fri, 11 May 2007 16:24 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030602050407040200050807
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Marko,

Please use the EMF newsgroup for questions about EMF itself. I've added
it to the "to" list on the reply.

Have you looked at the generated XyzExample that's produced in the
*.tests project when you do "Generate Test Code"? Should the package
for this EObject be marked as a non-namespace package (using the
annotation for null namespace schemas described in
http://www.eclipse.org/modeling/emf/docs/overviews/XMLSchema ToEcoreMapping.pdf)
or does it normally have a namespace? If the later, then using the
support added in https://bugs.eclipse.org/bugs/show_bug.cgi?id=166127
here's a way to save and load without a namespace:

public static void main(String[] args)
{
// Create a resource set to hold the resources.
//
ResourceSet resourceSet = new ResourceSetImpl();

// Register the appropriate resource factory to handle all file
extentions.
//

resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new LibraryResourceFactoryImpl()
{
@Override
public Resource createResource(URI uri)
{
XMLResource result = (XMLResource)super.createResource(uri);
ExtendedMetaData extendedMetaData =
new BasicExtendedMetaData()
{
@Override
public String getNamespace(EPackage ePackage)
{
return ePackage == LibraryPackage.eINSTANCE ? null
: super.getNamespace(ePackage);
}

@Override
protected boolean isFeatureNamespaceMatchingLax()
{
return true;
}
};
extendedMetaData.putPackage(null, LibraryPackage.eINSTANCE);

result.getDefaultLoadOptions().put(XMLResource.OPTION_EXTEND ED_META_DATA,
extendedMetaData);

result.getDefaultSaveOptions().put(XMLResource.OPTION_EXTEND ED_META_DATA,
extendedMetaData);

result.getDefaultSaveOptions().remove(XMLResource.OPTION_SCH EMA_LOCATION);
return result;
}
});

// Register the package to ensure it is available during loading.
//
resourceSet.getPackageRegistry().put
(LibraryPackage.eNS_URI,
LibraryPackage.eINSTANCE);

// If there are no arguments, emit an appropriate usage message.
//
if (args.length == 0)
{
System.out.println("Enter a list of file paths or URIs that
have content like this:");
try
{
Resource resource =
resourceSet.createResource(URI.createURI("http:///My.library"));
DocumentRoot documentRoot =
LibraryFactory.eINSTANCE.createDocumentRoot();
Library root = LibraryFactory.eINSTANCE.createLibrary();
documentRoot.setLibrary(root);
resource.getContents().add(documentRoot);
resource.save(System.out, null);
System.out.println();
ByteArrayOutputStream out = new ByteArrayOutputStream();
resource.save(out, null);
Resource resource2 =
resourceSet.createResource(URI.createURI("http:///My2.library"));
resource2.load(new ByteArrayInputStream(out.toByteArray()),
null);
resource2.save(System.out, null);
}
catch (IOException exception)
{
exception.printStackTrace();
}
}


Marko T. wrote:
> Hello folks,
> does anybody have an idea how to save an EObject to an xml file. The
> important thing here is, that the xml file must not have any namespace
> delarations/usages.
>
> Thanks in advance
>
> Greetings Marko
>


--------------030602050407040200050807
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Marko,<br>
<br>
Please use the EMF newsgroup for questions about EMF itself.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Saving EObject to namespaceless XML file
Next Topic:[ATL Transfromation] How to create a new class
Goto Forum:
  


Current Time: Wed Apr 24 15:11:58 GMT 2024

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

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

Back to the top