Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » save resource as instance of org.w3c.dom.Document
save resource as instance of org.w3c.dom.Document [message #959054] Fri, 26 October 2012 08:22 Go to next message
Marian Thieme is currently offline Marian Thieme
Messages: 11
Registered: August 2012
Junior Member
Hello,

I use the bookStore example from ibm (www.ibm.com/developerworks/library/os-eclipse-dynamicemf) to learn about emf xml serialization. I am interested in saving an Eobject as instance of org.w3c.dom.Document. I am trying this with the following code. This is actually the same as in DynamicModelSerializer.serializeModelInstance(), except I added ((XMLResource)resource).save(null, options, null).

...
ResourceSet resourceSet = new ResourceSetImpl();

// Register XML Factory implementation to handle .xml files
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
				.put("xml", new XMLResourceFactoryImpl());
// Create empty resource with the given URI
Resource resource = resourceSet.createResource(URI
				.createURI("./bookStore.xml"));

// Add bookStoreObject to contents list of the resource
resource.getContents().add(bookStoreEObject);
Map options = new HashMap();
options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);

try {
        //save the resource (to stdout)
	resource.save(System.out, options);
	//save as doc
	Document doc=((XMLResource)resource).save(null, options, null);
	Element el=doc.getDocumentElement();
	NodeList nodeList=el.getElementsByTagName("books");
	System.out.println(nodeList+nodeList.item(0));
}
...


The first save mehtod in the code above (the resource.save(System.out, options)) produces the output:
<bookStore:BookStore xmlns:bookStore="http:///com.ibm.dynamic.example.bookStore.ecore" owner="David Brown" location="Street#12, Top Town, NY">
  <books name="Harry Potter and the Deathly Hallows" isbn="157221"/>
</bookStore:BookStore>


Which seems to be a complete XML representation for BookStore object.


On the other hand, when using the XMLResource.save(...) method to save the xml as instance of org.w3c.dom.Document I only find an empty books element
com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@7c29e357 [books: null]


Why I cannot find the books elemen ?
Do I maybe only save for some reason the emf model rather than the instance of it ?

Thank you,
Marian
Re: save resource as instance of org.w3c.dom.Document [message #959246 is a reply to message #959054] Fri, 26 October 2012 11:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25917
Registered: July 2009
Senior Member
Marian,

I don't know how much meaning you should attach to the toString
representation of the NodeList implementation. Are you saying you
tested its getLength()?


On 26/10/2012 2:22 PM, Marian Thieme wrote:
> Hello,
>
> I use the bookStore example from ibm
> (www.ibm.com/developerworks/library/os-eclipse-dynamicemf) to learn
> about emf xml serialization. I am interested in saving an Eobject as
> instance of org.w3c.dom.Document. I am trying this with the following
> code. This is actually the same as in
> DynamicModelSerializer.serializeModelInstance(), except I added
> ((XMLResource)resource).save(null, options, null).
>
>
> ..
> ResourceSet resourceSet = new ResourceSetImpl();
>
> // Register XML Factory implementation to handle .xml files
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
> .put("xml", new XMLResourceFactoryImpl());
> // Create empty resource with the given URI
> Resource resource = resourceSet.createResource(URI
> .createURI("./bookStore.xml"));
>
> // Add bookStoreObject to contents list of the resource
> resource.getContents().add(bookStoreEObject);
> Map options = new HashMap();
> options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
>
> try {
> //save the resource (to stdout)
> resource.save(System.out, options);
> //save as doc
> Document doc=((XMLResource)resource).save(null, options, null);
> Element el=doc.getDocumentElement();
> NodeList nodeList=el.getElementsByTagName("books");
> System.out.println(nodeList+nodeList.item(0));
> }
> ..
>
>
> The first save mehtod in the code above (the resource.save(System.out,
> options)) produces the output:
>
> <bookStore:BookStore
> xmlns:bookStore="http:///com.ibm.dynamic.example.bookStore.ecore"
> owner="David Brown" location="Street#12, Top Town, NY">
> <books name="Harry Potter and the Deathly Hallows" isbn="157221"/>
> </bookStore:BookStore>
>
>
> Which seems to be a complete XML representation for BookStore object.
>
>
> On the other hand, when using the XMLResource.save(...) method to save
> the xml as instance of org.w3c.dom.Document I only find an empty books
> element
>
> com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@7c29e357
> [books: null]
>
>
> Why I cannot find the books elemen ?
> Do I maybe only save for some reason the emf model rather than the
> instance of it ?
>
> Thank you,
> Marian
Re: save resource as instance of org.w3c.dom.Document [message #959281 is a reply to message #959246] Fri, 26 October 2012 11:43 Go to previous message
Marian Thieme is currently offline Marian Thieme
Messages: 11
Registered: August 2012
Junior Member
Thanks Ed, my fault.
when inspecting the element more closely, I can find the attributes. What was printed, I guess, is the Node's tag name and value, the latter is apparently null.
Previous Topic:How to set a default value of an attribute in a subclass?
Next Topic:Ecore 2 Ecore - Could use some input
Goto Forum:
  


Current Time: Thu Jul 24 11:28:03 EDT 2014

Powered by FUDForum. Page generated in 0.05078 seconds