View on GitHub

JLibs

Common Utilities for Java

Replacing XML Namespaces

NamespaceReplacer extends org.xml.sax.helpers.XMLFilterImpl.

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:

Now create NamespaceReplacer:

import jlibs.xml.sax.NamespaceReplacer;

XMLReader reader = createNamespaceAwareSAXParser().getXMLReader();
reader = new NamespaceReplacer(reader, namespaces);
DefaultHandler myHandler = ...;
reader.parse(xmlFile, myHandler);

Now sax events recieved by myHandler will have namespaces replaced.