QName name = elem.getName();
@SuppressWarnings("unchecked")
Iterator<Namespace> it = elem.getNamespaces();
while (it.hasNext()) {
Namespace ns = it.next();
// First need to 'declare' namespace:
String prefix = ns.getPrefix();
if (prefix == null || prefix.length() == 0) {
setDefaultNamespace(ns.getNamespaceURI());
} else {
setPrefix(prefix, ns.getNamespaceURI());
}
}
/* Outputting element itself is fairly easy. The main question
* is whether namespaces match. Let's use simple heuristics:
* if writer is to do automatic prefix matching, let's only
* pass explicit prefix (not default one); otherwise we'll
* pass all parameters as is.
*/
/* Quick check first though: if URI part of QName is null, it's
* assumed element will just use whatever is current default
* namespace....
*/
String nsURI = name.getNamespaceURI();
if (nsURI == null) {
writeStartElement(name.getLocalPart());
} else {
String prefix = name.getPrefix();
writeStartElement(prefix, name.getLocalPart(), nsURI);
}
// And now we need to output namespaces (including default), if any:
@SuppressWarnings("unchecked")
Iterator<Namespace> it2 = elem.getNamespaces();
while (it2.hasNext()) {
Namespace ns = it2.next();
String prefix = ns.getPrefix();
if (prefix == null || prefix.length() == 0) {
writeDefaultNamespace(ns.getNamespaceURI());
} else {
writeNamespace(prefix, ns.getNamespaceURI());
}
}
// And finally, need to output attributes as well:
@SuppressWarnings("unchecked")