save resource as instance of org.w3c.dom.Document [message #959054] |
Fri, 26 October 2012 12:22 |
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 15:10 |
Ed Merks Messages: 33137 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
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
Powered by
FUDForum. Page generated in 0.01592 seconds