NamespaceReplacer - santhosh-tekuri/jlibs GitHub Wiki
NamespaceReplacer extends SAXDelegate.
SAXDelegate
is used to chain sax handlers. SAXDelegate
implements all sax handlers ContentHandler
, LexicalHandler
etc.
It provides setXXXHandler(…)
to support chaining.
First create a map specifying the namespaces to be replaced.
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/2003/05/soap-envelope");
namespaces.put("http://jlibs.org", "");
here we are trying to replace http://schemas.xmlsoap.org/soap/envelope/
with http://www.w3.org/2003/05/soap-envelope
and http://jlibs.org
with empty namespace.
Now create NamespaceReplacer
:
import jlibs.xml.sax.NamespaceReplacer;
NamespaceReplacer nsReplacer = new NamespaceReplacer(namespaces);
let us say you have MyDefaultHandler
which is supposed to recieve sax events.
Rather than registering MyDefaultHandler
with SAXParser
, you register nsReplacer
with SAXParser
, and MyDefaultHandler
is registered with nsReplacer
.
import javax.xml.parsers.SAXParser;
MyDefaultHandler myHandler = new MyDefaultHandler();
nsReplacer.setContentHandler(myHandler)
SAXParser parser = createNamespaceAwareSAXParser();
parser.parse(xmlFile, nsReplacer);
Now sax events recieved by myHandler
will have namespaces replaced.