On 18 Apr 2004, at 17:33, Nikolas Nehmer wrote:

The resulting XML file looked almost the same as the input XML file but no namespace declarations and no stylesheet definition. When I use the namespace attribute in the map-to element I can only define 1 single namespace but not several. Any suggestions?



We routinely do this for all of our created XML. We have a core XMLManager class which has marshal and unmarshal routines which look at the requested class (either a target Class object or the source Object class type to be marshalled) and pull some information from config to determine what should be done with those classes.


What we're doing in the main core of it ends up looking like this...

org.exolab.castor.xml.Marshaller m = new org.exolab.castor.xml.Marshaller(writer);
m.setMapping(mapping);
m.setValidation(cf.getOptionalBooleanValue(cfg, validateKey, true));
m.setMarshalExtendedType(cf.getOptionalBooleanValue(cfg, extendedTypeKey, false));
m.setMarshalAsDocument(cf.getOptionalBooleanValue(cfg, documentKey, true));


// Attach schemas.
if (schemaLocation!=null) { m.setSchemaLocation(schemaLocation); }
if (nnschemaLocation!=null) { m.setNoNamespaceSchemaLocation(nnschemaLocation); }
if (rootElement!=null) { m.setRootElement(rootElement); }


// Configure namespace mappings.
try {
        Config namespaceCfg = cf.getConfig(cfg, xmlNamespaceKey);
        Map map = namespaceCfg.getMap();
        if (map!=null) {
                Iterator it = map.keySet().iterator();
                while(it.hasNext()) {
                        String key = (String)it.next();
                        m.setNamespaceMapping(key, (String)map.get(key));
                }
        }
} catch (ConfigNotFoundException e) {
        // Not an error.
}
                                                
m.marshal(obj);
result = bos.toByteArray();


That's the long and short of it. We do a setNamespaceMapping() for each namespace we want to add to the list; we then include appropriate schemaLocation and noNamespaceSchemaLocation tags, and specify the root element as our namespaced root element.


In the mapping file, the <xml> tags all list the complete namespace to be written out. The end result is a fully namespaced and fully qualified, valid document.

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev




Reply via email to