problem loading xml docs with default namespace declared [message #399488] |
Tue, 21 March 2006 14:57  |
No real name Messages: 121 Registered: July 2009 |
Senior Member |
|
|
Hi,
I have an EMF model generated from XML Schema.
My program creates a model instance and saves it as XML, declaring a
default XML Namespace:
DocumentRoot docRoot =
ArticleFactory.eINSTANCE.createDocumentRoot();
docRoot.getXMLNSPrefixMap().put("",ArticlePackage.eNS_URI);
Loading that same XML file produces the root object instance, however
without content. That is, it contains no text, no child objects.
If I edit the XML document to include a namespace prefix, the loading
suddenly works. The Article object from the example above is
instantiated fully.
There is a subtlety here I am missing. Please help :)
Have fun at the conference.
Technical details are below in this message.
Thanks,
chris
Sample doc produced by save(), which when loaded, produces the empty
Article object :
<?xml version="1.0" encoding="ASCII"?>
<article xmlns="http://www.whatever.com/Article">
<title>The title</title>
<date>2006-03-21</date>
</article>
However adding namespace then works:
<?xml version="1.0" encoding="ASCII"?>
<art:article xmlns:art="http://www.whatever.com/Article">
<title>The title</title>
<date>2006-03-21</date>
</art:article>
Here is how I set up the resource for both load and save:
HashMap defSaveOptions = new HashMap(4);
defSaveOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE ,
new Boolean(true));
defSaveOptions.put(XMLResource.OPTION_EXTENDED_META_DATA,
new Boolean(true));
defSaveOptions.put(XMLResource.OPTION_DECLARE_XML,
new Boolean(true));
HashMap defLoadOptions = new HashMap(4);
defLoadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE ,
new Boolean(true));
defLoadOptions.put(XMLResource.OPTION_USE_LEXICAL_HANDLER,
Boolean.TRUE);
defLoadOptions.put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_ STYLE,
new Boolean(false));
ResourceSet resourceSet = new ResourceSetImpl();
// Register XML resource factory
XMLResourceFactoryImpl xmlResourceFactory =
new XMLResourceFactoryImpl();
resourceSet.getResourceFactoryRegistry().
getExtensionToFactoryMap().put("*",xmlResourceFactory);
// set namespace
resourceSet.getPackageRegistry().
put(ArticlePackage.eNS_URI, ArticlePackage.eINSTANCE);
resourceSet.getPackageRegistry().
put("", ArticlePackage.eINSTANCE);
XMLResource resource =
(XMLResource) resourceSet.createResource(uri);
resource.getDefaultLoadOptions().putAll(defLoadOptions);
resource.getDefaultSaveOptions().putAll(defSaveOptions);
|
|
|